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