tree.c revision 1.160 1 /* $NetBSD: tree.c,v 1.160 2021/01/16 18:48:52 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.160 2021/01/16 18:48:52 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 bool 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 *, bool);
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 *, bool);
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 (Tflag && strcmp(sym->s_name, "__lint_false") == 0) {
231 sym->s_scl = CTCONST; /* close enough */
232 sym->s_type = gettyp(BOOL);
233 sym->s_value.v_tspec = BOOL;
234 sym->s_value.v_ansiu = false;
235 sym->s_value.v_quad = 0;
236 } else if (Tflag &&
237 strcmp(sym->s_name, "__lint_true") == 0) {
238 sym->s_scl = CTCONST; /* close enough */
239 sym->s_type = gettyp(BOOL);
240 sym->s_value.v_tspec = BOOL;
241 sym->s_value.v_ansiu = false;
242 sym->s_value.v_quad = 1;
243 } else if (blklev == 0) {
244 /* %s undefined */
245 error(99, sym->s_name);
246 } else {
247 bool fixtype;
248 if (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
249 strcmp(sym->s_name, "__PRETTY_FUNCTION__")
250 == 0) {
251 /* __FUNCTION__/__PRETTY_FUNCTION... */
252 gnuism(316);
253 fixtype = true;
254 } else if (strcmp(sym->s_name, "__func__") == 0) {
255 if (!Sflag)
256 /* __func__ is a C9X feature */
257 warning(317);
258 fixtype = true;
259 } else {
260 /* %s undefined */
261 error(99, sym->s_name);
262 fixtype = false;
263 }
264 if (fixtype) {
265 sym->s_type = incref(gettyp(CHAR), PTR);
266 sym->s_type->t_const = true;
267 }
268 }
269 }
270 }
271
272 if (sym->s_kind != FVFT && sym->s_kind != FMEMBER)
273 LERROR("new_name_node(%d)", sym->s_kind);
274
275 n = getnode();
276 n->tn_type = sym->s_type;
277 if (sym->s_scl != CTCONST) {
278 n->tn_op = NAME;
279 n->tn_sym = sym;
280 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
281 n->tn_lvalue = true;
282 } else {
283 n->tn_op = CON;
284 n->tn_val = tgetblk(sizeof (val_t));
285 *n->tn_val = sym->s_value;
286 }
287
288 return n;
289 }
290
291 tnode_t *
292 new_string_node(strg_t *strg)
293 {
294 size_t len;
295 tnode_t *n;
296
297 len = strg->st_len;
298
299 n = getnode();
300
301 n->tn_op = STRING;
302 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY);
303 n->tn_type->t_dim = len + 1;
304 n->tn_lvalue = true;
305
306 n->tn_string = tgetblk(sizeof (strg_t));
307 n->tn_string->st_tspec = strg->st_tspec;
308 n->tn_string->st_len = len;
309
310 if (strg->st_tspec == CHAR) {
311 n->tn_string->st_cp = tgetblk(len + 1);
312 (void)memcpy(n->tn_string->st_cp, strg->st_cp, len + 1);
313 free(strg->st_cp);
314 } else {
315 n->tn_string->st_wcp = tgetblk((len + 1) * sizeof (wchar_t));
316 (void)memcpy(n->tn_string->st_wcp, strg->st_wcp,
317 (len + 1) * sizeof (wchar_t));
318 free(strg->st_wcp);
319 }
320 free(strg);
321
322 return n;
323 }
324
325 /*
326 * Returns a symbol which has the same name as the msym argument and is a
327 * member of the struct or union specified by the tn argument.
328 */
329 sym_t *
330 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
331 {
332 str_t *str;
333 type_t *tp;
334 sym_t *sym, *csym;
335 bool eq;
336 tspec_t t;
337
338 /*
339 * Remove the member if it was unknown until now (Which means
340 * that no defined struct or union has a member with the same name).
341 */
342 if (msym->s_scl == NOSCL) {
343 /* undefined struct/union member: %s */
344 error(101, msym->s_name);
345 rmsym(msym);
346 msym->s_kind = FMEMBER;
347 msym->s_scl = MOS;
348 msym->s_styp = tgetblk(sizeof (str_t));
349 msym->s_styp->stag = tgetblk(sizeof (sym_t));
350 msym->s_styp->stag->s_name = unnamed;
351 msym->s_value.v_tspec = INT;
352 return msym;
353 }
354
355 /* Set str to the tag of which msym is expected to be a member. */
356 str = NULL;
357 t = (tp = tn->tn_type)->t_tspec;
358 if (op == POINT) {
359 if (t == STRUCT || t == UNION)
360 str = tp->t_str;
361 } else if (op == ARROW && t == PTR) {
362 t = (tp = tp->t_subt)->t_tspec;
363 if (t == STRUCT || t == UNION)
364 str = tp->t_str;
365 }
366
367 /*
368 * If this struct/union has a member with the name of msym, return
369 * return this it.
370 */
371 if (str != NULL) {
372 for (sym = msym; sym != NULL; sym = sym->s_link) {
373 if (sym->s_scl != MOS && sym->s_scl != MOU)
374 continue;
375 if (sym->s_styp != str)
376 continue;
377 if (strcmp(sym->s_name, msym->s_name) != 0)
378 continue;
379 return sym;
380 }
381 }
382
383 /*
384 * Set eq to 0 if there are struct/union members with the same name
385 * and different types and/or offsets.
386 */
387 eq = true;
388 for (csym = msym; csym != NULL; csym = csym->s_link) {
389 if (csym->s_scl != MOS && csym->s_scl != MOU)
390 continue;
391 if (strcmp(msym->s_name, csym->s_name) != 0)
392 continue;
393 for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) {
394 bool w;
395
396 if (sym->s_scl != MOS && sym->s_scl != MOU)
397 continue;
398 if (strcmp(csym->s_name, sym->s_name) != 0)
399 continue;
400 if (csym->s_value.v_quad != sym->s_value.v_quad) {
401 eq = false;
402 break;
403 }
404 w = false;
405 eq = eqtype(csym->s_type, sym->s_type,
406 false, false, &w) && !w;
407 if (!eq)
408 break;
409 if (csym->s_bitfield != sym->s_bitfield) {
410 eq = false;
411 break;
412 }
413 if (csym->s_bitfield) {
414 type_t *tp1, *tp2;
415
416 tp1 = csym->s_type;
417 tp2 = sym->s_type;
418 if (tp1->t_flen != tp2->t_flen) {
419 eq = false;
420 break;
421 }
422 if (tp1->t_foffs != tp2->t_foffs) {
423 eq = false;
424 break;
425 }
426 }
427 }
428 if (!eq)
429 break;
430 }
431
432 /*
433 * Now handle the case in which the left operand refers really
434 * to a struct/union, but the right operand is not member of it.
435 */
436 if (str != NULL) {
437 if (eq && tflag) {
438 /* illegal member use: %s */
439 warning(102, msym->s_name);
440 } else {
441 /* illegal member use: %s */
442 error(102, msym->s_name);
443 }
444 return msym;
445 }
446
447 /*
448 * Now the left operand of ARROW does not point to a struct/union
449 * or the left operand of POINT is no struct/union.
450 */
451 if (eq) {
452 if (op == POINT) {
453 if (tflag) {
454 /* left operand of '.' must be struct/... */
455 warning(103);
456 } else {
457 /* left operand of '.' must be struct/... */
458 error(103);
459 }
460 } else {
461 /* left operand of "->" must be pointer to ... */
462 if (tflag && tn->tn_type->t_tspec == PTR) {
463 /* left operand of '->' must be pointer ... */
464 warning(104, type_name(tn->tn_type));
465 } else {
466 /* left operand of '->' must be pointer ... */
467 error(104, type_name(tn->tn_type));
468 }
469 }
470 } else {
471 if (tflag) {
472 /* non-unique member requires struct/union %s */
473 error(105, op == POINT ? "object" : "pointer");
474 } else {
475 /* unacceptable operand of '%s' */
476 error(111, modtab[op].m_name);
477 }
478 }
479
480 return msym;
481 }
482
483 /*
484 * Create a tree node. Called for most operands except function calls,
485 * sizeof and casts.
486 *
487 * op operator
488 * ln left operand
489 * rn if not NULL, right operand
490 */
491 tnode_t *
492 build(op_t op, tnode_t *ln, tnode_t *rn)
493 {
494 mod_t *mp;
495 tnode_t *ntn;
496 type_t *rtp;
497
498 mp = &modtab[op];
499
500 /* If there was an error in one of the operands, return. */
501 if (ln == NULL || (mp->m_binary && rn == NULL))
502 return NULL;
503
504 /*
505 * Apply class conversions to the left operand, but only if its
506 * value is needed or it is compared with null.
507 */
508 if (mp->m_vctx || mp->m_tctx)
509 ln = cconv(ln);
510 /*
511 * The right operand is almost always in a test or value context,
512 * except if it is a struct or union member.
513 */
514 if (mp->m_binary && op != ARROW && op != POINT)
515 rn = cconv(rn);
516
517 /*
518 * Print some warnings for comparisons of unsigned values with
519 * constants lower than or equal to null. This must be done
520 * before promote() because otherwise unsigned char and unsigned
521 * short would be promoted to int. Also types are tested to be
522 * CHAR, which would also become int.
523 */
524 if (mp->m_comp)
525 check_integer_comparison(op, ln, rn);
526
527 /*
528 * Promote the left operand if it is in a test or value context
529 */
530 if (mp->m_vctx || mp->m_tctx)
531 ln = promote(op, 0, ln);
532 /*
533 * Promote the right operand, but only if it is no struct or
534 * union member, or if it is not to be assigned to the left operand
535 */
536 if (mp->m_binary && op != ARROW && op != POINT &&
537 op != ASSIGN && op != RETURN) {
538 rn = promote(op, 0, rn);
539 }
540
541 /*
542 * If the result of the operation is different for signed or
543 * unsigned operands and one of the operands is signed only in
544 * ANSI C, print a warning.
545 */
546 if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) {
547 /* ANSI C treats constant as unsigned, op %s */
548 warning(218, mp->m_name);
549 ln->tn_val->v_ansiu = false;
550 }
551 if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) {
552 /* ANSI C treats constant as unsigned, op %s */
553 warning(218, mp->m_name);
554 rn->tn_val->v_ansiu = false;
555 }
556
557 /* Make sure both operands are of the same type */
558 if (mp->m_balance || (tflag && (op == SHL || op == SHR)))
559 balance(op, &ln, &rn);
560
561 /*
562 * Check types for compatibility with the operation and mutual
563 * compatibility. Return if there are serious problems.
564 */
565 if (!typeok(op, 0, ln, rn))
566 return NULL;
567
568 /* And now create the node. */
569 switch (op) {
570 case POINT:
571 case ARROW:
572 ntn = build_struct_access(op, ln, rn);
573 break;
574 case INCAFT:
575 case DECAFT:
576 case INCBEF:
577 case DECBEF:
578 ntn = build_prepost_incdec(op, ln);
579 break;
580 case AMPER:
581 ntn = build_ampersand(ln, 0);
582 break;
583 case STAR:
584 ntn = new_tnode(STAR, ln->tn_type->t_subt, ln, NULL);
585 break;
586 case PLUS:
587 case MINUS:
588 ntn = build_plus_minus(op, ln, rn);
589 break;
590 case SHL:
591 case SHR:
592 ntn = build_bit_shift(op, ln, rn);
593 break;
594 case COLON:
595 ntn = build_colon(ln, rn);
596 break;
597 case ASSIGN:
598 case MULASS:
599 case DIVASS:
600 case MODASS:
601 case ADDASS:
602 case SUBASS:
603 case SHLASS:
604 case SHRASS:
605 case ANDASS:
606 case XORASS:
607 case ORASS:
608 case RETURN:
609 ntn = build_assignment(op, ln, rn);
610 break;
611 case COMMA:
612 case QUEST:
613 ntn = new_tnode(op, rn->tn_type, ln, rn);
614 break;
615 case REAL:
616 case IMAG:
617 ntn = build_real_imag(op, ln);
618 break;
619 default:
620 rtp = mp->m_returns_bool
621 ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
622 lint_assert(mp->m_binary || rn == NULL);
623 ntn = new_tnode(op, rtp, ln, rn);
624 break;
625 }
626
627 /* Return if an error occurred. */
628 if (ntn == NULL)
629 return NULL;
630
631 /* Print a warning if precedence confusion is possible */
632 if (mp->m_possible_precedence_confusion)
633 check_precedence_confusion(ntn);
634
635 /*
636 * Print a warning if one of the operands is in a context where
637 * it is compared with null and if this operand is a constant.
638 */
639 if (mp->m_tctx) {
640 if (ln->tn_op == CON ||
641 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
642 if (hflag && !constcond_flag)
643 /* constant in conditional context */
644 warning(161);
645 }
646 }
647
648 /* Fold if the operator requires it */
649 if (mp->m_fold) {
650 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
651 if (mp->m_tctx) {
652 ntn = fold_test(ntn);
653 } else if (is_floating(ntn->tn_type->t_tspec)) {
654 ntn = fold_float(ntn);
655 } else {
656 ntn = fold(ntn);
657 }
658 } else if (op == QUEST && ln->tn_op == CON) {
659 ntn = ln->tn_val->v_quad != 0
660 ? rn->tn_left : rn->tn_right;
661 }
662 }
663
664 return ntn;
665 }
666
667 /*
668 * Perform class conversions.
669 *
670 * Arrays of type T are converted into pointers to type T.
671 * Functions are converted to pointers to functions.
672 * Lvalues are converted to rvalues.
673 */
674 tnode_t *
675 cconv(tnode_t *tn)
676 {
677 type_t *tp;
678
679 /*
680 * Array-lvalue (array of type T) is converted into rvalue
681 * (pointer to type T)
682 */
683 if (tn->tn_type->t_tspec == ARRAY) {
684 if (!tn->tn_lvalue) {
685 /* XXX print correct operator */
686 /* %soperand of '%s' must be lvalue */
687 gnuism(114, "", modtab[AMPER].m_name);
688 }
689 tn = new_tnode(AMPER, tincref(tn->tn_type->t_subt, PTR),
690 tn, NULL);
691 }
692
693 /*
694 * Expression of type function (function with return value of type T)
695 * in rvalue-expression (pointer to function with return value
696 * of type T)
697 */
698 if (tn->tn_type->t_tspec == FUNC)
699 tn = build_ampersand(tn, 1);
700
701 /* lvalue to rvalue */
702 if (tn->tn_lvalue) {
703 tp = tduptyp(tn->tn_type);
704 tp->t_const = tp->t_volatile = false;
705 tn = new_tnode(LOAD, tp, tn, NULL);
706 }
707
708 return tn;
709 }
710
711 static const tnode_t *
712 before_conversion(const tnode_t *tn)
713 {
714 while (tn->tn_op == CVT && !tn->tn_cast)
715 tn = tn->tn_left;
716 return tn;
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 /* For enums that are used as bit sets, allow "flags & FLAG". */
732 if (tn->tn_op == AND &&
733 tn->tn_left->tn_op == CVT &&
734 tn->tn_left->tn_type->t_tspec == INT && !tn->tn_left->tn_cast &&
735 tn->tn_left->tn_left->tn_type->t_tspec == ENUM &&
736 /*
737 * XXX: Somehow the type information got lost here. The type
738 * of the enum constant on the right-hand side should still be
739 * ENUM, but is INT.
740 */
741 tn->tn_right->tn_type->t_tspec == INT)
742 return true;
743
744 return false;
745 }
746
747 static bool
748 typeok_incdec(const mod_t *mp, const tnode_t *tn, const type_t *tp)
749 {
750 /* operand has scalar type (checked in typeok) */
751 if (!tn->tn_lvalue) {
752 if (tn->tn_op == CVT && tn->tn_cast &&
753 tn->tn_left->tn_op == LOAD) {
754 if (tn->tn_type->t_tspec == PTR)
755 return true;
756 /* a cast does not yield an lvalue */
757 error(163);
758 }
759 /* %soperand of '%s' must be lvalue */
760 error(114, "", mp->m_name);
761 return false;
762 } else if (tp->t_const) {
763 if (!tflag)
764 /* %soperand of '%s' must be modifiable ... */
765 warning(115, "", mp->m_name);
766 }
767 return true;
768 }
769
770 static bool
771 typeok_amper(const mod_t *mp,
772 const tnode_t *tn, const type_t *tp, tspec_t t)
773 {
774 if (t == ARRAY || t == FUNC) {
775 /* ok, a warning comes later (in build_ampersand()) */
776 } else if (!tn->tn_lvalue) {
777 if (tn->tn_op == CVT && tn->tn_cast &&
778 tn->tn_left->tn_op == LOAD) {
779 if (tn->tn_type->t_tspec == PTR)
780 return true;
781 /* a cast does not yield an lvalue */
782 error(163);
783 }
784 /* %soperand of '%s' must be lvalue */
785 error(114, "", mp->m_name);
786 return false;
787 } else if (is_scalar(t)) {
788 if (tp->t_bitfield) {
789 /* cannot take address of bit-field */
790 error(112);
791 return false;
792 }
793 } else if (t != STRUCT && t != UNION) {
794 /* unacceptable operand of '%s' */
795 error(111, mp->m_name);
796 return false;
797 }
798 if (tn->tn_op == NAME && tn->tn_sym->s_reg) {
799 /* cannot take address of register %s */
800 error(113, tn->tn_sym->s_name);
801 return false;
802 }
803 return true;
804 }
805
806 static bool
807 typeok_star(tspec_t t)
808 {
809 /* until now there were no type checks for this operator */
810 if (t != PTR) {
811 /* cannot dereference non-pointer type */
812 error(96);
813 return false;
814 }
815 return true;
816 }
817
818 static bool
819 typeok_plus(op_t op, tspec_t lt, tspec_t rt)
820 {
821 /* operands have scalar types (checked above) */
822 if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
823 warn_incompatible_types(op, lt, rt);
824 return false;
825 }
826 return true;
827 }
828
829 static bool
830 typeok_minus(op_t op,
831 const type_t *ltp, tspec_t lt,
832 const type_t *rtp, tspec_t rt)
833 {
834 /* operands have scalar types (checked above) */
835 if (lt == PTR && (!is_integer(rt) && rt != PTR)) {
836 warn_incompatible_types(op, lt, rt);
837 return false;
838 } else if (rt == PTR && lt != PTR) {
839 warn_incompatible_types(op, lt, rt);
840 return false;
841 }
842 if (lt == PTR && rt == PTR) {
843 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
844 /* illegal pointer subtraction */
845 error(116);
846 }
847 }
848 return true;
849 }
850
851 static void
852 typeok_shr(const mod_t *mp,
853 const tnode_t *ln, tspec_t lt,
854 const tnode_t *rn, tspec_t rt)
855 {
856 tspec_t olt, ort;
857
858 olt = before_conversion(ln)->tn_type->t_tspec;
859 ort = before_conversion(rn)->tn_type->t_tspec;
860
861 /* operands have integer types (checked above) */
862 if (pflag && !is_uinteger(lt)) {
863 /*
864 * The left operand is signed. This means that
865 * the operation is (possibly) nonportable.
866 */
867 if (ln->tn_op != CON) {
868 /* bitop. on signed value possibly nonportable */
869 warning(117);
870 } else if (ln->tn_val->v_quad < 0) {
871 /* bitop. on signed value nonportable */
872 warning(120);
873 }
874 } else if (!tflag && !sflag && !is_uinteger(olt) && is_uinteger(ort)) {
875 /*
876 * The left operand would become unsigned in
877 * traditional C.
878 */
879 if (hflag &&
880 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
881 /* semantics of '%s' change in ANSI C; ... */
882 warning(118, mp->m_name);
883 }
884 } else if (!tflag && !sflag && !is_uinteger(olt) && !is_uinteger(ort) &&
885 psize(lt) < psize(rt)) {
886 /*
887 * In traditional C the left operand would be extended,
888 * possibly with 1, and then shifted.
889 */
890 if (hflag &&
891 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
892 /* semantics of '%s' change in ANSI C; use ... */
893 warning(118, mp->m_name);
894 }
895 }
896 }
897
898 static void
899 typeok_shl(const mod_t *mp, tspec_t lt, tspec_t rt) {
900 /*
901 * ANSI C does not perform balancing for shift operations,
902 * but traditional C does. If the width of the right operand
903 * is greater than the width of the left operand, than in
904 * traditional C the left operand would be extended to the
905 * width of the right operand. For SHL this may result in
906 * different results.
907 */
908 if (psize(lt) < psize(rt)) {
909 /*
910 * XXX If both operands are constant, make sure
911 * that there is really a difference between
912 * ANSI C and traditional C.
913 */
914 if (hflag)
915 /* semantics of '%s' change in ANSI C; use ... */
916 warning(118, mp->m_name);
917 }
918 }
919
920 static void
921 typeok_shift(tspec_t lt, const tnode_t *rn, tspec_t rt)
922 {
923 if (rn->tn_op == CON) {
924 if (!is_uinteger(rt) && rn->tn_val->v_quad < 0) {
925 /* negative shift */
926 warning(121);
927 } else if ((uint64_t)rn->tn_val->v_quad == (uint64_t)size(lt)) {
928 /* shift equal to size of object */
929 warning(267);
930 } else if ((uint64_t)rn->tn_val->v_quad > (uint64_t)size(lt)) {
931 /* shift greater than size of object */
932 warning(122);
933 }
934 }
935 }
936
937 static bool
938 typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
939 {
940 if (lt == PTR && ((rt == PTR && rn->tn_type->t_tspec == VOID) ||
941 is_integer(rt))) {
942 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
943 return true;
944 }
945 if (rt == PTR && ((lt == PTR && ln->tn_type->t_tspec == VOID) ||
946 is_integer(lt))) {
947 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
948 return true;
949 }
950 return false;
951 }
952
953 static bool
954 typeok_ordered_comparison(op_t op, const mod_t *mp,
955 const tnode_t *ln, const type_t *ltp, tspec_t lt,
956 const tnode_t *rn, const type_t *rtp, tspec_t rt)
957 {
958 if ((lt == PTR || rt == PTR) && lt != rt) {
959 if (is_integer(lt) || is_integer(rt)) {
960 const char *lx = lt == PTR ?
961 "pointer" : "integer";
962 const char *rx = rt == PTR ?
963 "pointer" : "integer";
964 /* illegal combination of %s (%s) and ... */
965 warning(123, lx, type_name(ltp),
966 rx, type_name(rtp), mp->m_name);
967 } else {
968 warn_incompatible_types(op, lt, rt);
969 return false;
970 }
971 } else if (lt == PTR && rt == PTR) {
972 check_pointer_comparison(op, ln, rn);
973 }
974 return true;
975 }
976
977 static bool
978 typeok_quest(tspec_t lt, const tnode_t **rn)
979 {
980 if (!is_scalar(lt)) {
981 /* first operand must have scalar type, op ? : */
982 error(170);
983 return false;
984 }
985 while ((*rn)->tn_op == CVT)
986 *rn = (*rn)->tn_left;
987 lint_assert((*rn)->tn_op == COLON);
988 return true;
989 }
990
991 static bool
992 typeok_colon(const mod_t *mp,
993 const tnode_t *ln, const type_t *ltp, tspec_t lt,
994 const tnode_t *rn, const type_t *rtp, tspec_t rt)
995 {
996 type_t *lstp, *rstp;
997 tspec_t lst, rst;
998
999 if (is_arithmetic(lt) && is_arithmetic(rt))
1000 return true;
1001
1002 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
1003 return true;
1004 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
1005 return true;
1006
1007 lstp = lt == PTR ? ltp->t_subt : NULL;
1008 rstp = rt == PTR ? rtp->t_subt : NULL;
1009 lst = lstp != NULL ? lstp->t_tspec : NOTSPEC;
1010 rst = rstp != NULL ? rstp->t_tspec : NOTSPEC;
1011
1012 /* combination of any pointer and 0, 0L or (void *)0 is ok */
1013 if (lt == PTR && ((rt == PTR && rst == VOID) || is_integer(rt))) {
1014 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1015 return true;
1016 }
1017 if (rt == PTR && ((lt == PTR && lst == VOID) || is_integer(lt))) {
1018 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1019 return true;
1020 }
1021
1022 if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
1023 const char *lx = lt == PTR ? "pointer" : "integer";
1024 const char *rx = rt == PTR ? "pointer" : "integer";
1025 /* illegal combination of %s (%s) and %s (%s), op %s */
1026 warning(123, lx, type_name(ltp),
1027 rx, type_name(rtp), mp->m_name);
1028 return true;
1029 }
1030
1031 if (lt == VOID || rt == VOID) {
1032 if (lt != VOID || rt != VOID)
1033 /* incompatible types in conditional */
1034 warning(126);
1035 return true;
1036 }
1037
1038 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) ||
1039 (lst == FUNC && rst == VOID))) {
1040 /* (void *)0 handled above */
1041 if (sflag)
1042 /* ANSI C forbids conv. of %s to %s, op %s */
1043 warning(305, "function pointer", "'void *'",
1044 mp->m_name);
1045 return true;
1046 }
1047
1048 if (rt == PTR && lt == PTR) {
1049 if (eqptrtype(lstp, rstp, 1))
1050 return true;
1051 if (!eqtype(lstp, rstp, 1, 0, NULL))
1052 warn_incompatible_pointers(mp, ltp, rtp);
1053 return true;
1054 }
1055
1056 /* incompatible types in conditional */
1057 error(126);
1058 return false;
1059 }
1060
1061 static bool
1062 typeok_assign(const mod_t *mp, const tnode_t *ln, const type_t *ltp, tspec_t lt)
1063 {
1064 if (!ln->tn_lvalue) {
1065 if (ln->tn_op == CVT && ln->tn_cast &&
1066 ln->tn_left->tn_op == LOAD) {
1067 if (ln->tn_type->t_tspec == PTR)
1068 return true;
1069 /* a cast does not yield an lvalue */
1070 error(163);
1071 }
1072 /* %soperand of '%s' must be lvalue */
1073 error(114, "left ", mp->m_name);
1074 return false;
1075 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1076 has_constant_member(ltp))) {
1077 if (!tflag)
1078 /* %soperand of '%s' must be modifiable lvalue */
1079 warning(115, "left ", mp->m_name);
1080 }
1081 return true;
1082 }
1083
1084 /*
1085 * For assignment operators in strict bool mode, the type check is stricter
1086 * than for operators that compare to 0. Code that passes this strict check
1087 * can be compiled in a pre-C99 environment that doesn't implement the
1088 * special rule C99 6.3.1.2, without silent change in behavior.
1089 */
1090 static bool
1091 typeok_strict_bool_assign(op_t op, int arg,
1092 const tnode_t *ln, tspec_t lt,
1093 const tnode_t *rn, tspec_t rt)
1094 {
1095 if ((lt == BOOL) == (rt == BOOL))
1096 return true;
1097
1098 if (op == FARG) {
1099 /* argument #%d expects '%s', gets passed '%s' */
1100 error(334, arg, tspec_name(lt), tspec_name(rt));
1101 } else if (op == RETURN) {
1102 /* return value type mismatch (%s) and (%s) */
1103 error(211, type_name(ln->tn_type), type_name(rn->tn_type));
1104 return false;
1105 } else {
1106 /* operands of '%s' have incompatible types (%s != %s) */
1107 error(107, getopname(op), tspec_name(lt), tspec_name(rt));
1108 }
1109
1110 return false;
1111 }
1112
1113 /*
1114 * Whether the operator can handle (bool, bool) as well as (scalar, scalar),
1115 * but not mixtures between the two type classes.
1116 */
1117 static bool
1118 needs_compatible_types(op_t op)
1119 {
1120 return op == EQ || op == NE ||
1121 op == AND || op == XOR || op == OR ||
1122 op == COLON ||
1123 op == ASSIGN || op == ANDASS || op == XORASS || op == ORASS ||
1124 op == RETURN ||
1125 op == FARG;
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 (needs_compatible_types(op))
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 = 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 && 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 != NULL && 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 != NULL && 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 bool
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 true;
1499
1500 if (is_arithmetic(lt) && is_arithmetic(rt))
1501 return true;
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 true;
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 true;
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 true;
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 true;
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 false;
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 = true;
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, bool 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;
1896 bool u;
1897 type_t *ntp;
1898 static tspec_t tl[] = {
1899 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
1900 };
1901
1902 lt = (*lnp)->tn_type->t_tspec;
1903 rt = (*rnp)->tn_type->t_tspec;
1904
1905 if (!is_arithmetic(lt) || !is_arithmetic(rt))
1906 return;
1907
1908 if (!tflag) {
1909 if (lt == rt) {
1910 t = lt;
1911 } else if (lt == LCOMPLEX || rt == LCOMPLEX) {
1912 t = LCOMPLEX;
1913 } else if (lt == DCOMPLEX || rt == DCOMPLEX) {
1914 t = DCOMPLEX;
1915 } else if (lt == COMPLEX || rt == COMPLEX) {
1916 t = COMPLEX;
1917 } else if (lt == FCOMPLEX || rt == FCOMPLEX) {
1918 t = FCOMPLEX;
1919 } else if (lt == LDOUBLE || rt == LDOUBLE) {
1920 t = LDOUBLE;
1921 } else if (lt == DOUBLE || rt == DOUBLE) {
1922 t = DOUBLE;
1923 } else if (lt == FLOAT || rt == FLOAT) {
1924 t = FLOAT;
1925 } else {
1926 /*
1927 * If type A has more bits than type B it should
1928 * be able to hold all possible values of type B.
1929 */
1930 if (size(lt) > size(rt)) {
1931 t = lt;
1932 } else if (size(lt) < size(rt)) {
1933 t = rt;
1934 } else {
1935 for (i = 3; tl[i] != INT; i++) {
1936 if (tl[i] == lt || tl[i] == rt)
1937 break;
1938 }
1939 if ((is_uinteger(lt) || is_uinteger(rt)) &&
1940 !is_uinteger(tl[i])) {
1941 i--;
1942 }
1943 t = tl[i];
1944 }
1945 }
1946 } else {
1947 /* Keep unsigned in traditional C */
1948 u = is_uinteger(lt) || is_uinteger(rt);
1949 for (i = 0; tl[i] != INT; i++) {
1950 if (lt == tl[i] || rt == tl[i])
1951 break;
1952 }
1953 t = tl[i];
1954 if (u && is_integer(t) && !is_uinteger(t))
1955 t = unsigned_type(t);
1956 }
1957
1958 if (t != lt) {
1959 ntp = tduptyp((*lnp)->tn_type);
1960 ntp->t_tspec = t;
1961 *lnp = convert(op, 0, ntp, *lnp);
1962 }
1963 if (t != rt) {
1964 ntp = tduptyp((*rnp)->tn_type);
1965 ntp->t_tspec = t;
1966 *rnp = convert(op, 0, ntp, *rnp);
1967 }
1968 }
1969
1970 /*
1971 * Insert a conversion operator, which converts the type of the node
1972 * to another given type.
1973 * If op is FARG, arg is the number of the argument (used for warnings).
1974 */
1975 tnode_t *
1976 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
1977 {
1978 tnode_t *ntn;
1979 tspec_t nt, ot, ost = NOTSPEC;
1980
1981 nt = tp->t_tspec;
1982 if ((ot = tn->tn_type->t_tspec) == PTR)
1983 ost = tn->tn_type->t_subt->t_tspec;
1984
1985 if (!tflag && !sflag && op == FARG)
1986 check_prototype_conversion(arg, nt, ot, tp, tn);
1987 if (is_integer(nt) && is_integer(ot)) {
1988 check_integer_conversion(op, arg, nt, ot, tp, tn);
1989 } else if (nt == PTR && ((ot == PTR && ost == VOID) ||
1990 is_integer(ot)) && tn->tn_op == CON &&
1991 tn->tn_val->v_quad == 0) {
1992 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1993 } else if (is_integer(nt) && nt != BOOL && ot == PTR) {
1994 check_pointer_integer_conversion(op, nt, tp, tn);
1995 } else if (nt == PTR && ot == PTR) {
1996 check_pointer_conversion(op, tn, tp);
1997 }
1998
1999 ntn = getnode();
2000 ntn->tn_op = CVT;
2001 ntn->tn_type = tp;
2002 ntn->tn_cast = op == CVT;
2003 ntn->tn_right = NULL;
2004 if (tn->tn_op != CON || nt == VOID) {
2005 ntn->tn_left = tn;
2006 } else {
2007 ntn->tn_op = CON;
2008 ntn->tn_val = tgetblk(sizeof (val_t));
2009 convert_constant(op, arg, ntn->tn_type, ntn->tn_val,
2010 tn->tn_val);
2011 }
2012
2013 return ntn;
2014 }
2015
2016 /*
2017 * Print a warning if a prototype causes a type conversion that is
2018 * different from what would happen to the same argument in the
2019 * absence of a prototype.
2020 *
2021 * Errors/Warnings about illegal type combinations are already printed
2022 * in check_assign_types_compatible().
2023 */
2024 static void
2025 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
2026 tnode_t *tn)
2027 {
2028 tnode_t *ptn;
2029
2030 if (!is_arithmetic(nt) || !is_arithmetic(ot))
2031 return;
2032
2033 /*
2034 * If the type of the formal parameter is char/short, a warning
2035 * would be useless, because functions declared the old style
2036 * can't expect char/short arguments.
2037 */
2038 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
2039 return;
2040
2041 /* get default promotion */
2042 ptn = promote(NOOP, 1, tn);
2043 ot = ptn->tn_type->t_tspec;
2044
2045 /* return if types are the same with and without prototype */
2046 if (nt == ot || (nt == ENUM && ot == INT))
2047 return;
2048
2049 if (is_floating(nt) != is_floating(ot) ||
2050 psize(nt) != psize(ot)) {
2051 /* representation and/or width change */
2052 if (!is_integer(ot) || psize(ot) > psize(INT)) {
2053 /* conversion to '%s' due to prototype, arg #%d */
2054 warning(259, type_name(tp), arg);
2055 }
2056 } else if (hflag) {
2057 /*
2058 * they differ in sign or base type (char, short, int,
2059 * long, long long, float, double, long double)
2060 *
2061 * if they differ only in sign and the argument is a constant
2062 * and the msb of the argument is not set, print no warning
2063 */
2064 if (ptn->tn_op == CON && is_integer(nt) &&
2065 signed_type(nt) == signed_type(ot) &&
2066 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
2067 /* ok */
2068 } else {
2069 /* conversion to '%s' due to prototype, arg #%d */
2070 warning(259, type_name(tp), arg);
2071 }
2072 }
2073 }
2074
2075 /*
2076 * Print warnings for conversions of integer types which may cause problems.
2077 */
2078 /* ARGSUSED */
2079 static void
2080 check_integer_conversion(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp,
2081 tnode_t *tn)
2082 {
2083 char opbuf[16];
2084
2085 if (tn->tn_op == CON)
2086 return;
2087
2088 if (op == CVT)
2089 return;
2090
2091 if (Pflag && psize(nt) > psize(ot) &&
2092 is_uinteger(nt) != is_uinteger(ot)) {
2093 if (aflag > 0 && pflag) {
2094 if (op == FARG) {
2095 /* conversion to '%s' may sign-extend ... */
2096 warning(297, type_name(tp), arg);
2097 } else {
2098 /* conversion to '%s' may sign-extend ... */
2099 warning(131, type_name(tp));
2100 }
2101 }
2102 }
2103
2104 if (Pflag && psize(nt) > psize(ot)) {
2105 switch (tn->tn_op) {
2106 case PLUS:
2107 case MINUS:
2108 case MULT:
2109 case SHL:
2110 /* suggest cast from '%s' to '%s' on op %s to ... */
2111 warning(324, type_name(gettyp(ot)), type_name(tp),
2112 print_tnode(opbuf, sizeof(opbuf), tn));
2113 break;
2114 default:
2115 break;
2116 }
2117 }
2118
2119 if (psize(nt) < psize(ot) &&
2120 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
2121 aflag > 1)) {
2122 /* conversion from '%s' may lose accuracy */
2123 if (aflag > 0) {
2124 if (op == FARG) {
2125 /* conv. from '%s' to '%s' may lose ... */
2126 warning(298,
2127 type_name(tn->tn_type), type_name(tp), arg);
2128 } else {
2129 /* conv. from '%s' to '%s' may lose accuracy */
2130 warning(132,
2131 type_name(tn->tn_type), type_name(tp));
2132 }
2133 }
2134 }
2135 }
2136
2137 /*
2138 * Print warnings for dubious conversions of pointer to integer.
2139 */
2140 static void
2141 check_pointer_integer_conversion(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
2142 {
2143
2144 if (tn->tn_op == CON)
2145 return;
2146
2147 if (op != CVT) {
2148 /* We got already an error. */
2149 return;
2150 }
2151
2152 if (psize(nt) < psize(PTR)) {
2153 if (pflag && size(nt) >= size(PTR)) {
2154 /* conversion of pointer to '%s' may lose bits */
2155 warning(134, type_name(tp));
2156 } else {
2157 /* conversion of pointer to '%s' loses bits */
2158 warning(133, type_name(tp));
2159 }
2160 }
2161 }
2162
2163 /*
2164 * Print warnings for questionable pointer conversions.
2165 */
2166 static void
2167 check_pointer_conversion(op_t op, tnode_t *tn, type_t *tp)
2168 {
2169 tspec_t nt, ot;
2170 const char *nts, *ots;
2171
2172 /*
2173 * We got already an error (pointers of different types
2174 * without a cast) or we will not get a warning.
2175 */
2176 if (op != CVT)
2177 return;
2178
2179 nt = tp->t_subt->t_tspec;
2180 ot = tn->tn_type->t_subt->t_tspec;
2181
2182 if (nt == VOID || ot == VOID) {
2183 if (sflag && (nt == FUNC || ot == FUNC)) {
2184 /* (void *)0 already handled in convert() */
2185 *(nt == FUNC ? &nts : &ots) = "function pointer";
2186 *(nt == VOID ? &nts : &ots) = "'void *'";
2187 /* ANSI C forbids conversion of %s to %s */
2188 warning(303, ots, nts);
2189 }
2190 return;
2191 } else if (nt == FUNC && ot == FUNC) {
2192 return;
2193 } else if (nt == FUNC || ot == FUNC) {
2194 /* questionable conversion of function pointer */
2195 warning(229);
2196 return;
2197 }
2198
2199 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
2200 if (hflag)
2201 /* possible pointer alignment problem */
2202 warning(135);
2203 }
2204 if (((nt == STRUCT || nt == UNION) &&
2205 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
2206 psize(nt) != psize(ot)) {
2207 if (cflag) {
2208 /* pointer casts may be troublesome */
2209 warning(247);
2210 }
2211 }
2212 }
2213
2214 /*
2215 * Converts a typed constant in a constant of another type.
2216 *
2217 * op operator which requires conversion
2218 * arg if op is FARG, # of argument
2219 * tp type in which to convert the constant
2220 * nv new constant
2221 * v old constant
2222 */
2223 void
2224 convert_constant(op_t op, int arg, type_t *tp, val_t *nv, val_t *v)
2225 {
2226 tspec_t ot, nt;
2227 ldbl_t max = 0.0, min = 0.0;
2228 int sz;
2229 bool rchk;
2230 int64_t xmask, xmsk1;
2231 int osz, nsz;
2232
2233 ot = v->v_tspec;
2234 nt = nv->v_tspec = tp->t_tspec;
2235 rchk = false;
2236
2237 if (nt == BOOL) { /* C99 6.3.1.2 */
2238 nv->v_ansiu = false;
2239 nv->v_quad = is_nonzero_val(ot, v) ? 1 : 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*/false);
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 = true; /* 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 = false;
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 = false;
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 bool 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 = false;
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 = true;
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, bool 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 bool 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 = false;
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 = true;
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 = true;
3036 else if ((ul != 0) && ((q / ul) != ur))
3037 ovfl = true;
3038 } else {
3039 q = sl * sr;
3040 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
3041 ovfl = true;
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 = true;
3067 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
3068 if (msb(q, t, -1) != 0)
3069 ovfl = true;
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 = true;
3077 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
3078 if (msb(q, t, -1) != 0)
3079 ovfl = true;
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) ? 1 : 0;
3095 break;
3096 case LE:
3097 q = (utyp ? ul <= ur : sl <= sr) ? 1 : 0;
3098 break;
3099 case GE:
3100 q = (utyp ? ul >= ur : sl >= sr) ? 1 : 0;
3101 break;
3102 case GT:
3103 q = (utyp ? ul > ur : sl > sr) ? 1 : 0;
3104 break;
3105 case EQ:
3106 q = (utyp ? ul == ur : sl == sr) ? 1 : 0;
3107 break;
3108 case NE:
3109 q = (utyp ? ul != ur : sl != sr) ? 1 : 0;
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*/false);
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 bool l, r;
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 l = is_nonzero(tn->tn_left);
3153 r = modtab[tn->tn_op].m_binary && is_nonzero(tn->tn_right);
3154
3155 switch (tn->tn_op) {
3156 case NOT:
3157 if (hflag && !constcond_flag)
3158 /* constant argument to NOT */
3159 warning(239);
3160 v->v_quad = !l ? 1 : 0;
3161 break;
3162 case LOGAND:
3163 v->v_quad = l && r ? 1 : 0;
3164 break;
3165 case LOGOR:
3166 v->v_quad = l || r ? 1 : 0;
3167 break;
3168 default:
3169 lint_assert(/*CONSTCOND*/false);
3170 }
3171
3172 return new_constant_node(tn->tn_type, v);
3173 }
3174
3175 /*
3176 * Same for operands with floating point type.
3177 */
3178 static tnode_t *
3179 fold_float(tnode_t *tn)
3180 {
3181 val_t *v;
3182 tspec_t t;
3183 ldbl_t l, r = 0;
3184
3185 fpe = 0;
3186 v = xcalloc(1, sizeof (val_t));
3187 v->v_tspec = t = tn->tn_type->t_tspec;
3188
3189 lint_assert(is_floating(t));
3190 lint_assert(t == tn->tn_left->tn_type->t_tspec);
3191 lint_assert(!modtab[tn->tn_op].m_binary ||
3192 t == tn->tn_right->tn_type->t_tspec);
3193
3194 l = tn->tn_left->tn_val->v_ldbl;
3195 if (modtab[tn->tn_op].m_binary)
3196 r = tn->tn_right->tn_val->v_ldbl;
3197
3198 switch (tn->tn_op) {
3199 case UPLUS:
3200 v->v_ldbl = l;
3201 break;
3202 case UMINUS:
3203 v->v_ldbl = -l;
3204 break;
3205 case MULT:
3206 v->v_ldbl = l * r;
3207 break;
3208 case DIV:
3209 if (r == 0.0) {
3210 /* division by 0 */
3211 error(139);
3212 if (t == FLOAT) {
3213 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
3214 } else if (t == DOUBLE) {
3215 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
3216 } else {
3217 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
3218 }
3219 } else {
3220 v->v_ldbl = l / r;
3221 }
3222 break;
3223 case PLUS:
3224 v->v_ldbl = l + r;
3225 break;
3226 case MINUS:
3227 v->v_ldbl = l - r;
3228 break;
3229 case LT:
3230 v->v_quad = (l < r) ? 1 : 0;
3231 break;
3232 case LE:
3233 v->v_quad = (l <= r) ? 1 : 0;
3234 break;
3235 case GE:
3236 v->v_quad = (l >= r) ? 1 : 0;
3237 break;
3238 case GT:
3239 v->v_quad = (l > r) ? 1 : 0;
3240 break;
3241 case EQ:
3242 v->v_quad = (l == r) ? 1 : 0;
3243 break;
3244 case NE:
3245 v->v_quad = (l != r) ? 1 : 0;
3246 break;
3247 default:
3248 lint_assert(/*CONSTCOND*/false);
3249 }
3250
3251 lint_assert(fpe != 0 || isnan((double)v->v_ldbl) == false);
3252 if (fpe != 0 || finite((double)v->v_ldbl) == false ||
3253 (t == FLOAT &&
3254 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
3255 (t == DOUBLE &&
3256 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
3257 /* floating point overflow detected, op %s */
3258 warning(142, modtab[tn->tn_op].m_name);
3259 if (t == FLOAT) {
3260 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
3261 } else if (t == DOUBLE) {
3262 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
3263 } else {
3264 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
3265 }
3266 fpe = 0;
3267 }
3268
3269 return new_constant_node(tn->tn_type, v);
3270 }
3271
3272
3273 /*
3274 * Create a constant node for sizeof.
3275 */
3276 tnode_t *
3277 build_sizeof(type_t *tp)
3278 {
3279 tspec_t st;
3280 #if SIZEOF_IS_ULONG
3281 st = ULONG;
3282 #else
3283 st = UINT;
3284 #endif
3285 return new_integer_constant_node(st, tsize(tp) / CHAR_SIZE);
3286 }
3287
3288 /*
3289 * Create a constant node for offsetof.
3290 */
3291 tnode_t *
3292 build_offsetof(type_t *tp, sym_t *sym)
3293 {
3294 tspec_t st;
3295 #if SIZEOF_IS_ULONG
3296 st = ULONG;
3297 #else
3298 st = UINT;
3299 #endif
3300 tspec_t t = tp->t_tspec;
3301 if (t != STRUCT && t != UNION)
3302 /* unacceptable operand of '%s' */
3303 error(111, "offsetof");
3304
3305 // XXX: wrong size, no checking for sym fixme
3306 return new_integer_constant_node(st, tsize(tp) / CHAR_SIZE);
3307 }
3308
3309 int64_t
3310 tsize(type_t *tp)
3311 {
3312 int elem, elsz;
3313 bool flex;
3314
3315 elem = 1;
3316 flex = false;
3317 while (tp->t_tspec == ARRAY) {
3318 flex = true; /* allow c99 flex arrays [] [0] */
3319 elem *= tp->t_dim;
3320 tp = tp->t_subt;
3321 }
3322 if (elem == 0) {
3323 if (!flex) {
3324 /* cannot take size/alignment of incomplete type */
3325 error(143);
3326 elem = 1;
3327 }
3328 }
3329 switch (tp->t_tspec) {
3330 case FUNC:
3331 /* cannot take size/alignment of function */
3332 error(144);
3333 elsz = 1;
3334 break;
3335 case STRUCT:
3336 case UNION:
3337 if (incompl(tp)) {
3338 /* cannot take size/alignment of incomplete type */
3339 error(143);
3340 elsz = 1;
3341 } else {
3342 elsz = tp->t_str->size;
3343 }
3344 break;
3345 case ENUM:
3346 if (incompl(tp)) {
3347 /* cannot take size/alignment of incomplete type */
3348 warning(143);
3349 }
3350 /* FALLTHROUGH */
3351 default:
3352 if (tp->t_bitfield) {
3353 /* cannot take size/alignment of bit-field */
3354 error(145);
3355 }
3356 if (tp->t_tspec == VOID) {
3357 /* cannot take size/alignment of void */
3358 error(146);
3359 elsz = 1;
3360 } else {
3361 elsz = size(tp->t_tspec);
3362 lint_assert(elsz > 0);
3363 }
3364 break;
3365 }
3366
3367 /* XXX: type conversion is too late */
3368 return (int64_t)(elem * elsz);
3369 }
3370
3371 /*
3372 */
3373 tnode_t *
3374 build_alignof(type_t *tp)
3375 {
3376 tspec_t st;
3377
3378 switch (tp->t_tspec) {
3379 case ARRAY:
3380 break;
3381
3382 case FUNC:
3383 /* cannot take size/alignment of function */
3384 error(144);
3385 return 0;
3386
3387 case STRUCT:
3388 case UNION:
3389 if (incompl(tp)) {
3390 /* cannot take size/alignment of incomplete type */
3391 error(143);
3392 return 0;
3393 }
3394 break;
3395 case ENUM:
3396 break;
3397 default:
3398 if (tp->t_bitfield) {
3399 /* cannot take size/alignment of bit-field */
3400 error(145);
3401 return 0;
3402 }
3403 if (tp->t_tspec == VOID) {
3404 /* cannot take size/alignment of void */
3405 error(146);
3406 return 0;
3407 }
3408 break;
3409 }
3410
3411 #if SIZEOF_IS_ULONG
3412 st = ULONG;
3413 #else
3414 st = UINT;
3415 #endif
3416
3417 return new_integer_constant_node(st, (int64_t)getbound(tp) / CHAR_SIZE);
3418 }
3419
3420 /*
3421 * Type casts.
3422 */
3423 tnode_t *
3424 cast(tnode_t *tn, type_t *tp)
3425 {
3426 tspec_t nt, ot;
3427
3428 if (tn == NULL)
3429 return NULL;
3430
3431 tn = cconv(tn);
3432
3433 nt = tp->t_tspec;
3434 ot = tn->tn_type->t_tspec;
3435
3436 if (nt == VOID) {
3437 /*
3438 * XXX ANSI C requires scalar types or void (Plauger & Brodie).
3439 * But this seams really questionable.
3440 */
3441 } else if (nt == UNION) {
3442 sym_t *m;
3443 str_t *str = tp->t_str;
3444 if (!Sflag) {
3445 /* union cast is a C9X feature */
3446 error(328);
3447 return NULL;
3448 }
3449 for (m = str->memb; m != NULL; m = m->s_next) {
3450 if (sametype(m->s_type, tn->tn_type)) {
3451 tn = getnode();
3452 tn->tn_op = CVT;
3453 tn->tn_type = tp;
3454 tn->tn_cast = true;
3455 tn->tn_right = NULL;
3456 return tn;
3457 }
3458 }
3459 /* type '%s' is not a member of '%s' */
3460 error(329, type_name(tn->tn_type), type_name(tp));
3461 return NULL;
3462 } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
3463 if (!Sflag || nt == ARRAY || nt == FUNC) {
3464 /* invalid cast expression */
3465 error(147);
3466 return NULL;
3467 }
3468 } else if (ot == STRUCT || ot == UNION) {
3469 /* invalid cast expression */
3470 error(147);
3471 return NULL;
3472 } else if (ot == VOID) {
3473 /* improper cast of void expression */
3474 error(148);
3475 return NULL;
3476 } else if (is_integer(nt) && is_scalar(ot)) {
3477 /* ok */
3478 } else if (is_floating(nt) && is_arithmetic(ot)) {
3479 /* ok */
3480 } else if (nt == PTR && is_integer(ot)) {
3481 /* ok */
3482 } else if (nt == PTR && ot == PTR) {
3483 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3484 if (hflag)
3485 /* cast discards 'const' from ... */
3486 warning(275);
3487 }
3488 } else {
3489 /* invalid cast expression */
3490 error(147);
3491 return NULL;
3492 }
3493
3494 tn = convert(CVT, 0, tp, tn);
3495 tn->tn_cast = true;
3496
3497 return tn;
3498 }
3499
3500 /*
3501 * Create the node for a function argument.
3502 * All necessary conversions and type checks are done in
3503 * new_function_call_node because new_function_argument_node has no
3504 * information about expected argument types.
3505 */
3506 tnode_t *
3507 new_function_argument_node(tnode_t *args, tnode_t *arg)
3508 {
3509 tnode_t *ntn;
3510
3511 /*
3512 * If there was a serious error in the expression for the argument,
3513 * create a dummy argument so the positions of the remaining arguments
3514 * will not change.
3515 */
3516 if (arg == NULL)
3517 arg = new_integer_constant_node(INT, (int64_t)0);
3518
3519 ntn = new_tnode(PUSH, arg->tn_type, arg, args);
3520
3521 return ntn;
3522 }
3523
3524 /*
3525 * Create the node for a function call. Also check types of
3526 * function arguments and insert conversions, if necessary.
3527 */
3528 tnode_t *
3529 new_function_call_node(tnode_t *func, tnode_t *args)
3530 {
3531 tnode_t *ntn;
3532 op_t fcop;
3533
3534 if (func == NULL)
3535 return NULL;
3536
3537 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3538 fcop = CALL;
3539 } else {
3540 fcop = ICALL;
3541 }
3542
3543 /*
3544 * after cconv() func will always be a pointer to a function
3545 * if it is a valid function designator.
3546 */
3547 func = cconv(func);
3548
3549 if (func->tn_type->t_tspec != PTR ||
3550 func->tn_type->t_subt->t_tspec != FUNC) {
3551 /* illegal function (type %s) */
3552 error(149, type_name(func->tn_type));
3553 return NULL;
3554 }
3555
3556 args = check_function_arguments(func->tn_type->t_subt, args);
3557
3558 ntn = new_tnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3559
3560 return ntn;
3561 }
3562
3563 /*
3564 * Check types of all function arguments and insert conversions,
3565 * if necessary.
3566 */
3567 static tnode_t *
3568 check_function_arguments(type_t *ftp, tnode_t *args)
3569 {
3570 tnode_t *arg;
3571 sym_t *asym;
3572 tspec_t at;
3573 int narg, npar, n, i;
3574
3575 /* get # of args in the prototype */
3576 npar = 0;
3577 for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
3578 npar++;
3579
3580 /* get # of args in function call */
3581 narg = 0;
3582 for (arg = args; arg != NULL; arg = arg->tn_right)
3583 narg++;
3584
3585 asym = ftp->t_args;
3586 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3587 /* argument mismatch: %d arg%s passed, %d expected */
3588 error(150, narg, narg > 1 ? "s" : "", npar);
3589 asym = NULL;
3590 }
3591
3592 for (n = 1; n <= narg; n++) {
3593
3594 /*
3595 * The rightmost argument is at the top of the argument
3596 * subtree.
3597 */
3598 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3599 continue;
3600
3601 /* some things which are always not allowed */
3602 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3603 /* void expressions may not be arguments, arg #%d */
3604 error(151, n);
3605 return NULL;
3606 } else if ((at == STRUCT || at == UNION) &&
3607 incompl(arg->tn_left->tn_type)) {
3608 /* argument cannot have unknown size, arg #%d */
3609 error(152, n);
3610 return NULL;
3611 } else if (is_integer(at) &&
3612 arg->tn_left->tn_type->t_isenum &&
3613 incompl(arg->tn_left->tn_type)) {
3614 /* argument cannot have unknown size, arg #%d */
3615 warning(152, n);
3616 }
3617
3618 /* class conversions (arg in value context) */
3619 arg->tn_left = cconv(arg->tn_left);
3620
3621 if (asym != NULL) {
3622 arg->tn_left = check_prototype_argument(
3623 n, asym->s_type, arg->tn_left);
3624 } else {
3625 arg->tn_left = promote(NOOP, 1, arg->tn_left);
3626 }
3627 arg->tn_type = arg->tn_left->tn_type;
3628
3629 if (asym != NULL)
3630 asym = asym->s_next;
3631 }
3632
3633 return args;
3634 }
3635
3636 /*
3637 * Compare the type of an argument with the corresponding type of a
3638 * prototype parameter. If it is a valid combination, but both types
3639 * are not the same, insert a conversion to convert the argument into
3640 * the type of the parameter.
3641 */
3642 static tnode_t *
3643 check_prototype_argument(
3644 int n, /* pos of arg */
3645 type_t *tp, /* expected type (from prototype) */
3646 tnode_t *tn) /* argument */
3647 {
3648 tnode_t *ln;
3649 bool dowarn;
3650
3651 ln = xcalloc(1, sizeof (tnode_t));
3652 ln->tn_type = tduptyp(tp);
3653 ln->tn_type->t_const = false;
3654 ln->tn_lvalue = true;
3655 if (typeok(FARG, n, ln, tn)) {
3656 if (!eqtype(tp, tn->tn_type,
3657 true, false, (dowarn = false, &dowarn)) || dowarn)
3658 tn = convert(FARG, n, tp, tn);
3659 }
3660 free(ln);
3661 return tn;
3662 }
3663
3664 /*
3665 * Return the value of an integral constant expression.
3666 * If the expression is not constant or its type is not an integer
3667 * type, an error message is printed.
3668 */
3669 val_t *
3670 constant(tnode_t *tn, bool required)
3671 {
3672 val_t *v;
3673
3674 if (tn != NULL)
3675 tn = cconv(tn);
3676 if (tn != NULL)
3677 tn = promote(NOOP, 0, tn);
3678
3679 v = xcalloc(1, sizeof (val_t));
3680
3681 if (tn == NULL) {
3682 lint_assert(nerr != 0);
3683 if (dflag)
3684 printf("constant node is null; returning 1 instead\n");
3685 v->v_tspec = INT;
3686 v->v_quad = 1;
3687 return v;
3688 }
3689
3690 v->v_tspec = tn->tn_type->t_tspec;
3691
3692 if (tn->tn_op == CON) {
3693 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
3694 if (is_integer(tn->tn_val->v_tspec)) {
3695 v->v_ansiu = tn->tn_val->v_ansiu;
3696 v->v_quad = tn->tn_val->v_quad;
3697 return v;
3698 }
3699 v->v_quad = tn->tn_val->v_ldbl;
3700 } else {
3701 v->v_quad = 1;
3702 }
3703
3704 if (required)
3705 /* integral constant expression expected */
3706 error(55);
3707 else
3708 /* variable array dimension is a C99/GCC extension */
3709 c99ism(318);
3710
3711 if (!is_integer(v->v_tspec))
3712 v->v_tspec = INT;
3713
3714 return v;
3715 }
3716
3717 /*
3718 * Perform some tests on expressions which can't be done in build() and
3719 * functions called by build(). These tests must be done here because
3720 * we need some information about the context in which the operations
3721 * are performed.
3722 * After all tests are performed, expr() frees the memory which is used
3723 * for the expression.
3724 */
3725 void
3726 expr(tnode_t *tn, bool vctx, bool tctx, bool dofreeblk)
3727 {
3728
3729 lint_assert(tn != NULL || nerr != 0);
3730
3731 if (tn == NULL) {
3732 tfreeblk();
3733 return;
3734 }
3735
3736 /* expr() is also called in global initialisations */
3737 if (dcs->d_ctx != EXTERN)
3738 check_statement_reachable();
3739
3740 check_expr_misc(tn, vctx, tctx, !tctx, 0, 0, 0);
3741 if (tn->tn_op == ASSIGN) {
3742 if (hflag && tctx)
3743 /* assignment in conditional context */
3744 warning(159);
3745 } else if (tn->tn_op == CON) {
3746 if (hflag && tctx && !constcond_flag)
3747 /* constant in conditional context */
3748 warning(161);
3749 }
3750 if (!modtab[tn->tn_op].m_sideeff) {
3751 /*
3752 * for left operands of COMMA this warning is already
3753 * printed
3754 */
3755 if (tn->tn_op != COMMA && !vctx && !tctx)
3756 check_null_effect(tn);
3757 }
3758 if (dflag)
3759 display_expression(tn, 0);
3760
3761 /* free the tree memory */
3762 if (dofreeblk)
3763 tfreeblk();
3764 }
3765
3766 static void
3767 check_null_effect(const tnode_t *tn)
3768 {
3769
3770 if (!hflag)
3771 return;
3772
3773 while (!modtab[tn->tn_op].m_sideeff) {
3774 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3775 tn = tn->tn_left;
3776 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3777 /*
3778 * && and || have a side effect if the right operand
3779 * has a side effect.
3780 */
3781 tn = tn->tn_right;
3782 } else if (tn->tn_op == QUEST) {
3783 /*
3784 * ? has a side effect if at least one of its right
3785 * operands has a side effect
3786 */
3787 tn = tn->tn_right;
3788 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) {
3789 /*
3790 * : has a side effect if at least one of its operands
3791 * has a side effect
3792 */
3793 if (modtab[tn->tn_left->tn_op].m_sideeff) {
3794 tn = tn->tn_left;
3795 } else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3796 tn = tn->tn_right;
3797 } else {
3798 break;
3799 }
3800 } else {
3801 break;
3802 }
3803 }
3804 if (!modtab[tn->tn_op].m_sideeff)
3805 /* expression has null effect */
3806 warning(129);
3807 }
3808
3809 /*
3810 * Dump an expression to stdout
3811 * only used for debugging
3812 */
3813 static void
3814 display_expression(const tnode_t *tn, int offs)
3815 {
3816 uint64_t uq;
3817
3818 if (tn == NULL) {
3819 (void)printf("%*s%s\n", offs, "", "NULL");
3820 return;
3821 }
3822 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
3823
3824 if (tn->tn_op == NAME) {
3825 (void)printf("%s: %s ",
3826 tn->tn_sym->s_name,
3827 storage_class_name(tn->tn_sym->s_scl));
3828 } else if (tn->tn_op == CON && is_floating(tn->tn_type->t_tspec)) {
3829 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3830 } else if (tn->tn_op == CON && is_integer(tn->tn_type->t_tspec)) {
3831 uq = tn->tn_val->v_quad;
3832 (void)printf("0x %08lx %08lx ",
3833 (long)(uq >> 32) & 0xffffffffl,
3834 (long)uq & 0xffffffffl);
3835 } else if (tn->tn_op == CON) {
3836 lint_assert(tn->tn_type->t_tspec == PTR);
3837 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3838 (u_long)tn->tn_val->v_quad);
3839 } else if (tn->tn_op == STRING) {
3840 if (tn->tn_string->st_tspec == CHAR) {
3841 (void)printf("\"%s\"", tn->tn_string->st_cp);
3842 } else {
3843 char *s;
3844 size_t n;
3845 n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
3846 s = xmalloc(n);
3847 (void)wcstombs(s, tn->tn_string->st_wcp, n);
3848 (void)printf("L\"%s\"", s);
3849 free(s);
3850 }
3851 (void)printf(" ");
3852 } else if (tn->tn_op == FSEL) {
3853 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3854 tn->tn_type->t_flen);
3855 }
3856 (void)printf("%s\n", ttos(tn->tn_type));
3857 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3858 return;
3859 display_expression(tn->tn_left, offs + 2);
3860 if (modtab[tn->tn_op].m_binary ||
3861 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3862 display_expression(tn->tn_right, offs + 2);
3863 }
3864 }
3865
3866 /*
3867 * Called by expr() to recursively perform some tests.
3868 */
3869 /* ARGSUSED */
3870 void
3871 check_expr_misc(const tnode_t *tn, bool vctx, bool tctx,
3872 bool eqwarn, bool fcall, bool rvdisc, bool szof)
3873 {
3874 tnode_t *ln, *rn;
3875 mod_t *mp;
3876 bool nrvdisc, cvctx, ctctx;
3877 op_t op;
3878 scl_t sc;
3879 dinfo_t *di;
3880
3881 if (tn == NULL)
3882 return;
3883
3884 ln = tn->tn_left;
3885 rn = tn->tn_right;
3886 mp = &modtab[op = tn->tn_op];
3887
3888 switch (op) {
3889 case AMPER:
3890 if (ln->tn_op == NAME && (reached || rchflg)) {
3891 if (!szof)
3892 mark_as_set(ln->tn_sym);
3893 mark_as_used(ln->tn_sym, fcall, szof);
3894 }
3895 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3896 /* check the range of array indices */
3897 check_array_index(ln->tn_left, 1);
3898 break;
3899 case LOAD:
3900 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3901 /* check the range of array indices */
3902 check_array_index(ln->tn_left, 0);
3903 /* FALLTHROUGH */
3904 case PUSH:
3905 case INCBEF:
3906 case DECBEF:
3907 case INCAFT:
3908 case DECAFT:
3909 case ADDASS:
3910 case SUBASS:
3911 case MULASS:
3912 case DIVASS:
3913 case MODASS:
3914 case ANDASS:
3915 case ORASS:
3916 case XORASS:
3917 case SHLASS:
3918 case SHRASS:
3919 case REAL:
3920 case IMAG:
3921 if (ln->tn_op == NAME && (reached || rchflg)) {
3922 sc = ln->tn_sym->s_scl;
3923 /*
3924 * Look if there was a asm statement in one of the
3925 * compound statements we are in. If not, we don't
3926 * print a warning.
3927 */
3928 for (di = dcs; di != NULL; di = di->d_next) {
3929 if (di->d_asm)
3930 break;
3931 }
3932 if (sc != EXTERN && sc != STATIC &&
3933 !ln->tn_sym->s_set && !szof && di == NULL) {
3934 /* %s may be used before set */
3935 warning(158, ln->tn_sym->s_name);
3936 mark_as_set(ln->tn_sym);
3937 }
3938 mark_as_used(ln->tn_sym, 0, 0);
3939 }
3940 break;
3941 case ASSIGN:
3942 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3943 mark_as_set(ln->tn_sym);
3944 if (ln->tn_sym->s_scl == EXTERN)
3945 outusg(ln->tn_sym);
3946 }
3947 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3948 /* check the range of array indices */
3949 check_array_index(ln->tn_left, 0);
3950 break;
3951 case CALL:
3952 lint_assert(ln->tn_op == AMPER);
3953 lint_assert(ln->tn_left->tn_op == NAME);
3954 if (!szof)
3955 outcall(tn, vctx || tctx, rvdisc);
3956 break;
3957 case EQ:
3958 if (hflag && eqwarn)
3959 /* operator '==' found where '=' was expected */
3960 warning(160);
3961 break;
3962 case CON:
3963 case NAME:
3964 case STRING:
3965 return;
3966 /* LINTED206: (enumeration values not handled in switch) */
3967 case OR:
3968 case XOR:
3969 case NE:
3970 case GE:
3971 case GT:
3972 case LE:
3973 case LT:
3974 case SHR:
3975 case SHL:
3976 case MINUS:
3977 case PLUS:
3978 case MOD:
3979 case DIV:
3980 case MULT:
3981 case STAR:
3982 case UMINUS:
3983 case UPLUS:
3984 case DEC:
3985 case INC:
3986 case COMPL:
3987 case NOT:
3988 case POINT:
3989 case ARROW:
3990 case NOOP:
3991 case AND:
3992 case FARG:
3993 case CASE:
3994 case INIT:
3995 case RETURN:
3996 case ICALL:
3997 case CVT:
3998 case COMMA:
3999 case FSEL:
4000 case COLON:
4001 case QUEST:
4002 case LOGOR:
4003 case LOGAND:
4004 break;
4005 }
4006
4007 cvctx = mp->m_vctx;
4008 ctctx = mp->m_tctx;
4009 /*
4010 * values of operands of ':' are not used if the type of at least
4011 * one of the operands (for gcc compatibility) is void
4012 * XXX test/value context of QUEST should probably be used as
4013 * context for both operands of COLON
4014 */
4015 if (op == COLON && tn->tn_type->t_tspec == VOID)
4016 cvctx = ctctx = false;
4017 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
4018 check_expr_misc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
4019
4020 switch (op) {
4021 case PUSH:
4022 if (rn != NULL)
4023 check_expr_misc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
4024 break;
4025 case LOGAND:
4026 case LOGOR:
4027 check_expr_misc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
4028 break;
4029 case COLON:
4030 check_expr_misc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
4031 break;
4032 case COMMA:
4033 check_expr_misc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof);
4034 break;
4035 default:
4036 if (mp->m_binary)
4037 check_expr_misc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
4038 break;
4039 }
4040
4041 }
4042
4043 /*
4044 * Checks the range of array indices, if possible.
4045 * amper is set if only the address of the element is used. This
4046 * means that the index is allowed to refer to the first element
4047 * after the array.
4048 */
4049 static void
4050 check_array_index(tnode_t *tn, bool amper)
4051 {
4052 int dim;
4053 tnode_t *ln, *rn;
4054 int elsz;
4055 int64_t con;
4056
4057 ln = tn->tn_left;
4058 rn = tn->tn_right;
4059
4060 /* We can only check constant indices. */
4061 if (rn->tn_op != CON)
4062 return;
4063
4064 /* Return if the left node does not stem from an array. */
4065 if (ln->tn_op != AMPER)
4066 return;
4067 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
4068 return;
4069 if (ln->tn_left->tn_type->t_tspec != ARRAY)
4070 return;
4071
4072 /*
4073 * For incomplete array types, we can print a warning only if
4074 * the index is negative.
4075 */
4076 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
4077 return;
4078
4079 /* Get the size of one array element */
4080 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
4081 return;
4082 elsz /= CHAR_SIZE;
4083
4084 /* Change the unit of the index from bytes to element size. */
4085 if (is_uinteger(rn->tn_type->t_tspec)) {
4086 con = (uint64_t)rn->tn_val->v_quad / elsz;
4087 } else {
4088 con = rn->tn_val->v_quad / elsz;
4089 }
4090
4091 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
4092
4093 if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
4094 /* array subscript cannot be negative: %ld */
4095 warning(167, (long)con);
4096 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
4097 /* array subscript cannot be > %d: %ld */
4098 warning(168, dim - 1, (long)con);
4099 }
4100 }
4101
4102 /*
4103 * Check for ordered comparisons of unsigned values with 0.
4104 */
4105 static void
4106 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
4107 {
4108 tspec_t lt, rt;
4109 mod_t *mp;
4110
4111 lt = ln->tn_type->t_tspec;
4112 rt = rn->tn_type->t_tspec;
4113 mp = &modtab[op];
4114
4115 if (ln->tn_op != CON && rn->tn_op != CON)
4116 return;
4117
4118 if (!is_integer(lt) || !is_integer(rt))
4119 return;
4120
4121 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
4122 (rn->tn_val->v_quad < 0 ||
4123 rn->tn_val->v_quad > (int)~(~0U << (CHAR_SIZE - 1)))) {
4124 /* nonportable character comparison, op %s */
4125 warning(230, mp->m_name);
4126 return;
4127 }
4128 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
4129 (ln->tn_val->v_quad < 0 ||
4130 ln->tn_val->v_quad > (int)~(~0U << (CHAR_SIZE - 1)))) {
4131 /* nonportable character comparison, op %s */
4132 warning(230, mp->m_name);
4133 return;
4134 }
4135 if (is_uinteger(lt) && !is_uinteger(rt) &&
4136 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
4137 if (rn->tn_val->v_quad < 0) {
4138 /* comparison of %s with %s, op %s */
4139 warning(162, type_name(ln->tn_type),
4140 "negative constant", mp->m_name);
4141 } else if (op == LT || op == GE || (hflag && op == LE)) {
4142 /* comparison of %s with %s, op %s */
4143 warning(162, type_name(ln->tn_type), "0", mp->m_name);
4144 }
4145 return;
4146 }
4147 if (is_uinteger(rt) && !is_uinteger(lt) &&
4148 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
4149 if (ln->tn_val->v_quad < 0) {
4150 /* comparison of %s with %s, op %s */
4151 warning(162, "negative constant",
4152 type_name(rn->tn_type), mp->m_name);
4153 } else if (op == GT || op == LE || (hflag && op == GE)) {
4154 /* comparison of %s with %s, op %s */
4155 warning(162, "0", type_name(rn->tn_type), mp->m_name);
4156 }
4157 return;
4158 }
4159 }
4160
4161 /*
4162 * Takes an expression an returns 0 if this expression can be used
4163 * for static initialisation, otherwise -1.
4164 *
4165 * Constant initialisation expressions must be constant or an address
4166 * of a static object with an optional offset. In the first case,
4167 * the result is returned in *offsp. In the second case, the static
4168 * object is returned in *symp and the offset in *offsp.
4169 *
4170 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
4171 * CON. Type conversions are allowed if they do not change binary
4172 * representation (including width).
4173 */
4174 int
4175 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
4176 {
4177 sym_t *sym;
4178 ptrdiff_t offs1, offs2;
4179 tspec_t t, ot;
4180
4181 switch (tn->tn_op) {
4182 case MINUS:
4183 if (tn->tn_right->tn_op == CVT)
4184 return conaddr(tn->tn_right, symp, offsp);
4185 else if (tn->tn_right->tn_op != CON)
4186 return -1;
4187 /* FALLTHROUGH */
4188 case PLUS:
4189 offs1 = offs2 = 0;
4190 if (tn->tn_left->tn_op == CON) {
4191 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
4192 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
4193 return -1;
4194 } else if (tn->tn_right->tn_op == CON) {
4195 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
4196 if (tn->tn_op == MINUS)
4197 offs2 = -offs2;
4198 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
4199 return -1;
4200 } else {
4201 return -1;
4202 }
4203 *symp = sym;
4204 *offsp = offs1 + offs2;
4205 break;
4206 case AMPER:
4207 if (tn->tn_left->tn_op == NAME) {
4208 *symp = tn->tn_left->tn_sym;
4209 *offsp = 0;
4210 } else if (tn->tn_left->tn_op == STRING) {
4211 /*
4212 * If this would be the front end of a compiler we
4213 * would return a label instead of 0.
4214 */
4215 *offsp = 0;
4216 }
4217 break;
4218 case CVT:
4219 t = tn->tn_type->t_tspec;
4220 ot = tn->tn_left->tn_type->t_tspec;
4221 if ((!is_integer(t) && t != PTR) ||
4222 (!is_integer(ot) && ot != PTR)) {
4223 return -1;
4224 }
4225 #ifdef notdef
4226 /*
4227 * consider:
4228 * struct foo {
4229 * unsigned char a;
4230 * } f = {
4231 * (u_char)(u_long)(&(((struct foo *)0)->a))
4232 * };
4233 * since psize(u_long) != psize(u_char) this fails.
4234 */
4235 else if (psize(t) != psize(ot))
4236 return -1;
4237 #endif
4238 if (conaddr(tn->tn_left, symp, offsp) == -1)
4239 return -1;
4240 break;
4241 default:
4242 return -1;
4243 }
4244 return 0;
4245 }
4246
4247 /*
4248 * Concatenate two string constants.
4249 */
4250 strg_t *
4251 cat_strings(strg_t *strg1, strg_t *strg2)
4252 {
4253 size_t len1, len2, len;
4254
4255 if (strg1->st_tspec != strg2->st_tspec) {
4256 /* cannot concatenate wide and regular string literals */
4257 error(292);
4258 return strg1;
4259 }
4260
4261 len1 = strg1->st_len;
4262 len2 = strg2->st_len + 1; /* + NUL */
4263 len = len1 + len2;
4264
4265 #define COPY(F) \
4266 do { \
4267 strg1->F = xrealloc(strg1->F, len * sizeof(*strg1->F)); \
4268 (void)memcpy(strg1->F + len1, strg2->F, len2 * sizeof(*strg1->F)); \
4269 free(strg2->F); \
4270 } while (/*CONSTCOND*/false)
4271
4272 if (strg1->st_tspec == CHAR)
4273 COPY(st_cp);
4274 else
4275 COPY(st_wcp);
4276
4277 strg1->st_len = len - 1; /* - NUL */
4278 free(strg2);
4279
4280 return strg1;
4281 }
4282
4283 static bool
4284 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
4285 {
4286
4287 if (op == SHL || op == SHR) {
4288 if (!lparen && (lop == PLUS || lop == MINUS))
4289 return true;
4290 if (!rparen && (rop == PLUS || rop == MINUS))
4291 return true;
4292 return false;
4293 }
4294
4295 if (op == LOGOR) {
4296 if (!lparen && lop == LOGAND)
4297 return true;
4298 if (!rparen && rop == LOGAND)
4299 return true;
4300 return false;
4301 }
4302
4303 lint_assert(op == AND || op == XOR || op == OR);
4304 if (!lparen && lop != op) {
4305 if (lop == PLUS || lop == MINUS)
4306 return true;
4307 if (lop == AND || lop == XOR)
4308 return true;
4309 }
4310 if (!rparen && rop != op) {
4311 if (rop == PLUS || rop == MINUS)
4312 return true;
4313 if (rop == AND || rop == XOR)
4314 return true;
4315 }
4316 return false;
4317 }
4318
4319 /*
4320 * Print a warning if the given node has operands which should be
4321 * parenthesized.
4322 *
4323 * XXX Does not work if an operand is a constant expression. Constant
4324 * expressions are already folded.
4325 */
4326 static void
4327 check_precedence_confusion(tnode_t *tn)
4328 {
4329 tnode_t *ln, *rn;
4330
4331 if (!hflag)
4332 return;
4333
4334 dprint_node(tn);
4335
4336 lint_assert(modtab[tn->tn_op].m_binary);
4337 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4338 continue;
4339 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
4340 continue;
4341
4342 if (is_confusing_precedence(tn->tn_op,
4343 ln->tn_op, ln->tn_parenthesized,
4344 rn->tn_op, rn->tn_parenthesized)) {
4345 /* precedence confusion possible: parenthesize! */
4346 warning(169);
4347 }
4348 }
4349