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