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