tree.c revision 1.161 1 /* $NetBSD: tree.c,v 1.161 2021/01/16 18:58:21 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.161 2021/01/16 18:58:21 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 return typeok_strict_bool_compatible(op, arg, lt, rt);
1149
1150 if (mp->m_takes_only_bool || op == QUEST) {
1151 bool binary = mp->m_binary;
1152 bool lbool = is_strict_bool(ln);
1153 bool ok = true;
1154
1155 if (!binary && !lbool) {
1156 /* operand of '%s' must be bool, not '%s' */
1157 error(330, getopname(op), tspec_name(lt));
1158 ok = false;
1159 }
1160 if (binary && !lbool) {
1161 /* left operand of '%s' must be bool, not '%s' */
1162 error(331, getopname(op), tspec_name(lt));
1163 ok = false;
1164 }
1165 if (binary && op != QUEST && !is_strict_bool(rn)) {
1166 /* right operand of '%s' must be bool, not '%s' */
1167 error(332, getopname(op), tspec_name(rt));
1168 ok = false;
1169 }
1170 return ok;
1171 }
1172
1173 if (!mp->m_takes_bool) {
1174 bool binary = mp->m_binary;
1175 bool lbool = ln->tn_type->t_tspec == BOOL;
1176 bool ok = true;
1177
1178 if (!binary && lbool) {
1179 /* operand of '%s' must not be bool */
1180 error(335, getopname(op));
1181 ok = false;
1182 }
1183 if (binary && lbool) {
1184 /* left operand of '%s' must not be bool */
1185 error(336, getopname(op));
1186 ok = false;
1187 }
1188 if (binary && rn->tn_type->t_tspec == BOOL) {
1189 /* right operand of '%s' must not be bool */
1190 error(337, getopname(op));
1191 ok = false;
1192 }
1193 return ok;
1194 }
1195
1196 return true;
1197 }
1198
1199 /* Check the types using the information from modtab[]. */
1200 static bool
1201 typeok_scalar(op_t op, const mod_t *mp,
1202 const tnode_t *ln, tspec_t lt,
1203 const tnode_t *rn, tspec_t rt)
1204 {
1205 if (mp->m_requires_integer) {
1206 if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
1207 warn_incompatible_types(op, lt, rt);
1208 return false;
1209 }
1210 } else if (mp->m_requires_integer_or_complex) {
1211 if ((!is_integer(lt) && !is_complex(lt)) ||
1212 (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
1213 warn_incompatible_types(op, lt, rt);
1214 return false;
1215 }
1216 } else if (mp->m_requires_scalar) {
1217 if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
1218 warn_incompatible_types(op, lt, rt);
1219 return false;
1220 }
1221 } else if (mp->m_requires_arith) {
1222 if (!is_arithmetic(lt) ||
1223 (mp->m_binary && !is_arithmetic(rt))) {
1224 warn_incompatible_types(op, lt, rt);
1225 return false;
1226 }
1227 }
1228 return true;
1229 }
1230
1231 /* Check the types for specific operators and type combinations. */
1232 static bool
1233 typeok_op(op_t op, const mod_t *mp, int arg,
1234 const tnode_t *ln, const type_t *ltp, tspec_t lt,
1235 const tnode_t *rn, const type_t *rtp, tspec_t rt)
1236 {
1237 switch (op) {
1238 case POINT:
1239 /*
1240 * Most errors required by ANSI C are reported in
1241 * struct_or_union_member().
1242 * Here we only must check for totally wrong things.
1243 */
1244 if (lt == FUNC || lt == VOID || ltp->t_bitfield ||
1245 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
1246 /* Without tflag we got already an error */
1247 if (tflag)
1248 /* unacceptable operand of '%s' */
1249 error(111, mp->m_name);
1250 return false;
1251 }
1252 /* Now we have an object we can create a pointer to */
1253 break;
1254 case ARROW:
1255 if (lt != PTR && !(tflag && is_integer(lt))) {
1256 /* Without tflag we got already an error */
1257 if (tflag)
1258 /* unacceptable operand of '%s' */
1259 error(111, mp->m_name);
1260 return false;
1261 }
1262 break;
1263 case INCAFT:
1264 case DECAFT:
1265 case INCBEF:
1266 case DECBEF:
1267 if (!typeok_incdec(mp, ln, ltp))
1268 return false;
1269 break;
1270 case AMPER:
1271 if (!typeok_amper(mp, ln, ltp, lt))
1272 return false;
1273 break;
1274 case STAR:
1275 if (!typeok_star(lt))
1276 return false;
1277 break;
1278 case PLUS:
1279 if (!typeok_plus(op, lt, rt))
1280 return false;
1281 break;
1282 case MINUS:
1283 if (!typeok_minus(op, ltp, lt, rtp, rt))
1284 return false;
1285 break;
1286 case SHR:
1287 typeok_shr(mp, ln, lt, rn, rt);
1288 goto shift;
1289 case SHL:
1290 typeok_shl(mp, lt, rt);
1291 shift:
1292 typeok_shift(lt, rn, rt);
1293 break;
1294 case EQ:
1295 case NE:
1296 /*
1297 * Accept some things which are allowed with EQ and NE,
1298 * but not with ordered comparisons.
1299 */
1300 if (typeok_eq(ln, lt, rn, rt))
1301 break;
1302 /* FALLTHROUGH */
1303 case LT:
1304 case GT:
1305 case LE:
1306 case GE:
1307 if (!typeok_ordered_comparison(op, mp,
1308 ln, ltp, lt, rn, rtp, rt))
1309 return false;
1310 break;
1311 case QUEST:
1312 if (!typeok_quest(lt, &rn))
1313 return false;
1314 break;
1315 case COLON:
1316 if (!typeok_colon(mp, ln, ltp, lt, rn, rtp, rt))
1317 return false;
1318 break;
1319 case ASSIGN:
1320 case INIT:
1321 case FARG:
1322 case RETURN:
1323 if (!check_assign_types_compatible(op, arg, ln, rn))
1324 return false;
1325 goto assign;
1326 case MULASS:
1327 case DIVASS:
1328 case MODASS:
1329 goto assign;
1330 case ADDASS:
1331 case SUBASS:
1332 /* operands have scalar types (checked above) */
1333 if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
1334 warn_incompatible_types(op, lt, rt);
1335 return false;
1336 }
1337 goto assign;
1338 case SHLASS:
1339 goto assign;
1340 case SHRASS:
1341 if (pflag && !is_uinteger(lt) && !(tflag && is_uinteger(rt))) {
1342 /* bitop. on signed value possibly nonportable */
1343 warning(117);
1344 }
1345 goto assign;
1346 case ANDASS:
1347 case XORASS:
1348 case ORASS:
1349 goto assign;
1350 assign:
1351 if (!typeok_assign(mp, ln, ltp, lt))
1352 return false;
1353 break;
1354 case COMMA:
1355 if (!modtab[ln->tn_op].m_sideeff)
1356 check_null_effect(ln);
1357 break;
1358 /* LINTED206: (enumeration values not handled in switch) */
1359 case CON:
1360 case CASE:
1361 case PUSH:
1362 case LOAD:
1363 case ICALL:
1364 case CVT:
1365 case CALL:
1366 case FSEL:
1367 case STRING:
1368 case NAME:
1369 case LOGOR:
1370 case LOGAND:
1371 case OR:
1372 case XOR:
1373 case AND:
1374 case MOD:
1375 case DIV:
1376 case MULT:
1377 case UMINUS:
1378 case UPLUS:
1379 case DEC:
1380 case INC:
1381 case COMPL:
1382 case NOT:
1383 case NOOP:
1384 case REAL:
1385 case IMAG:
1386 break;
1387 }
1388 return true;
1389 }
1390
1391 static void
1392 typeok_enum(op_t op, const mod_t *mp, int arg,
1393 const tnode_t *ln, const type_t *ltp,
1394 const tnode_t *rn, const type_t *rtp)
1395 {
1396 if (mp->m_bad_on_enum &&
1397 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) {
1398 check_bad_enum_operation(op, ln, rn);
1399 } else if (mp->m_valid_on_enum &&
1400 (ltp->t_isenum && rtp != NULL && rtp->t_isenum)) {
1401 check_enum_type_mismatch(op, arg, ln, rn);
1402 } else if (mp->m_valid_on_enum &&
1403 (ltp->t_isenum || (rtp != NULL && rtp->t_isenum))) {
1404 check_enum_int_mismatch(op, arg, ln, rn);
1405 }
1406 }
1407
1408 /* Perform most type checks. Return whether the types are ok. */
1409 bool
1410 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
1411 {
1412 mod_t *mp;
1413 tspec_t lt, rt;
1414 type_t *ltp, *rtp;
1415
1416 mp = &modtab[op];
1417
1418 lint_assert((ltp = ln->tn_type) != NULL);
1419 lt = ltp->t_tspec;
1420
1421 if (mp->m_binary) {
1422 lint_assert((rtp = rn->tn_type) != NULL);
1423 rt = rtp->t_tspec;
1424 } else {
1425 rtp = NULL;
1426 rt = NOTSPEC;
1427 }
1428
1429 if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
1430 return false;
1431 if (!typeok_scalar(op, mp, ln, lt, rn, rt))
1432 return false;
1433
1434 if (!typeok_op(op, mp, arg, ln, ltp, lt, rn, rtp, rt))
1435 return false;
1436
1437 typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
1438 return true;
1439 }
1440
1441 static void
1442 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
1443 {
1444 type_t *ltp, *rtp;
1445 tspec_t lt, rt;
1446 const char *lts, *rts;
1447
1448 lt = (ltp = ln->tn_type)->t_subt->t_tspec;
1449 rt = (rtp = rn->tn_type)->t_subt->t_tspec;
1450
1451 if (lt == VOID || rt == VOID) {
1452 if (sflag && (lt == FUNC || rt == FUNC)) {
1453 /* (void *)0 already handled in typeok() */
1454 *(lt == FUNC ? <s : &rts) = "function pointer";
1455 *(lt == VOID ? <s : &rts) = "'void *'";
1456 /* ANSI C forbids comparison of %s with %s */
1457 warning(274, lts, rts);
1458 }
1459 return;
1460 }
1461
1462 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
1463 warn_incompatible_pointers(&modtab[op], ltp, rtp);
1464 return;
1465 }
1466
1467 if (lt == FUNC && rt == FUNC) {
1468 if (sflag && op != EQ && op != NE)
1469 /* ANSI C forbids ordered comparisons of ... */
1470 warning(125);
1471 }
1472 }
1473
1474 /*
1475 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1476 * and prints warnings/errors if necessary.
1477 * If the types are (almost) compatible, 1 is returned, otherwise 0.
1478 */
1479 static bool
1480 check_assign_types_compatible(op_t op, int arg,
1481 const tnode_t *ln, const tnode_t *rn)
1482 {
1483 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC;
1484 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
1485 mod_t *mp;
1486 const char *lts, *rts;
1487
1488 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
1489 lst = (lstp = ltp->t_subt)->t_tspec;
1490 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
1491 rst = (rstp = rtp->t_subt)->t_tspec;
1492 mp = &modtab[op];
1493
1494 if (lt == BOOL && is_scalar(rt)) /* C99 6.3.1.2 */
1495 return true;
1496
1497 if (is_arithmetic(lt) && is_arithmetic(rt))
1498 return true;
1499
1500 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
1501 /* both are struct or union */
1502 return ltp->t_str == rtp->t_str;
1503
1504 /* 0, 0L and (void *)0 may be assigned to any pointer */
1505 if (lt == PTR && ((rt == PTR && rst == VOID) || is_integer(rt))) {
1506 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1507 return true;
1508 }
1509
1510 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
1511 /* two pointers, at least one pointer to void */
1512 if (sflag && (lst == FUNC || rst == FUNC)) {
1513 /* comb. of ptr to func and ptr to void */
1514 *(lst == FUNC ? <s : &rts) = "function pointer";
1515 *(lst == VOID ? <s : &rts) = "'void *'";
1516 switch (op) {
1517 case INIT:
1518 case RETURN:
1519 /* ANSI C forbids conversion of %s to %s */
1520 warning(303, rts, lts);
1521 break;
1522 case FARG:
1523 /* ANSI C forbids conv. of %s to %s, arg #%d */
1524 warning(304, rts, lts, arg);
1525 break;
1526 default:
1527 /* ANSI C forbids conv. of %s to %s, op %s */
1528 warning(305, rts, lts, mp->m_name);
1529 break;
1530 }
1531 }
1532 }
1533
1534 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
1535 eqtype(lstp, rstp, 1, 0, NULL))) {
1536 /* compatible pointer types (qualifiers ignored) */
1537 if (!tflag &&
1538 ((!lstp->t_const && rstp->t_const) ||
1539 (!lstp->t_volatile && rstp->t_volatile))) {
1540 /* left side has not all qualifiers of right */
1541 switch (op) {
1542 case INIT:
1543 case RETURN:
1544 /* incompatible pointer types (%s != %s) */
1545 warning(182, type_name(lstp), type_name(rstp));
1546 break;
1547 case FARG:
1548 /* argument has incompatible pointer type... */
1549 warning(153,
1550 arg, type_name(lstp), type_name(rstp));
1551 break;
1552 default:
1553 /* operands have incompatible pointer type... */
1554 warning(128, mp->m_name,
1555 type_name(lstp), type_name(rstp));
1556 break;
1557 }
1558 }
1559 return true;
1560 }
1561
1562 if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
1563 const char *lx = lt == PTR ? "pointer" : "integer";
1564 const char *rx = rt == PTR ? "pointer" : "integer";
1565
1566 switch (op) {
1567 case INIT:
1568 case RETURN:
1569 /* illegal combination of %s (%s) and %s (%s) */
1570 warning(183, lx, type_name(ltp), rx, type_name(rtp));
1571 break;
1572 case FARG:
1573 /* illegal comb. of %s (%s) and %s (%s), arg #%d */
1574 warning(154,
1575 lx, type_name(ltp), rx, type_name(rtp), arg);
1576 break;
1577 default:
1578 /* illegal combination of %s (%s) and %s (%s), op %s */
1579 warning(123,
1580 lx, type_name(ltp), rx, type_name(rtp), mp->m_name);
1581 break;
1582 }
1583 return true;
1584 }
1585
1586 if (lt == PTR && rt == PTR) {
1587 switch (op) {
1588 case INIT:
1589 case RETURN:
1590 warn_incompatible_pointers(NULL, ltp, rtp);
1591 break;
1592 case FARG:
1593 /* arg. has incomp. pointer type, arg #%d (%s != %s) */
1594 warning(153, arg, type_name(ltp), type_name(rtp));
1595 break;
1596 default:
1597 warn_incompatible_pointers(mp, ltp, rtp);
1598 break;
1599 }
1600 return true;
1601 }
1602
1603 switch (op) {
1604 case INIT:
1605 /* initialisation type mismatch (%s) and (%s) */
1606 error(185, type_name(ltp), type_name(rtp));
1607 break;
1608 case RETURN:
1609 /* return value type mismatch (%s) and (%s) */
1610 error(211, type_name(ltp), type_name(rtp));
1611 break;
1612 case FARG:
1613 /* argument is incompatible with prototype, arg #%d */
1614 warning(155, arg);
1615 break;
1616 default:
1617 warn_incompatible_types(op, lt, rt);
1618 break;
1619 }
1620
1621 return false;
1622 }
1623
1624 /*
1625 * Prints a warning if an operator, which should be senseless for an
1626 * enum type, is applied to an enum type.
1627 */
1628 static void
1629 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
1630 {
1631 mod_t *mp;
1632
1633 if (!eflag)
1634 return;
1635
1636 mp = &modtab[op];
1637
1638 if (!(ln->tn_type->t_isenum ||
1639 (mp->m_binary && rn->tn_type->t_isenum))) {
1640 return;
1641 }
1642
1643 /*
1644 * Enum as offset to a pointer is an exception (otherwise enums
1645 * could not be used as array indices).
1646 */
1647 if (op == PLUS &&
1648 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) ||
1649 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) {
1650 return;
1651 }
1652
1653 /* dubious operation on enum, op %s */
1654 warning(241, mp->m_name);
1655
1656 }
1657
1658 /*
1659 * Prints a warning if an operator is applied to two different enum types.
1660 */
1661 static void
1662 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
1663 {
1664 mod_t *mp;
1665
1666 mp = &modtab[op];
1667
1668 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
1669 switch (op) {
1670 case INIT:
1671 /* enum type mismatch in initialisation */
1672 warning(210);
1673 break;
1674 case FARG:
1675 /* enum type mismatch, arg #%d (%s != %s) */
1676 warning(156, arg,
1677 type_name(ln->tn_type), type_name(rn->tn_type));
1678 break;
1679 case RETURN:
1680 /* return value type mismatch (%s) and (%s) */
1681 warning(211,
1682 type_name(ln->tn_type), type_name(rn->tn_type));
1683 break;
1684 default:
1685 /* enum type mismatch, op %s */
1686 warning(130, mp->m_name);
1687 break;
1688 }
1689 } else if (Pflag && mp->m_comp && op != EQ && op != NE) {
1690 if (eflag)
1691 /* dubious comparison of enums, op %s */
1692 warning(243, mp->m_name);
1693 }
1694 }
1695
1696 /*
1697 * Prints a warning if an operator has both enum and other integer
1698 * types.
1699 */
1700 static void
1701 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
1702 {
1703
1704 if (!eflag)
1705 return;
1706
1707 switch (op) {
1708 case INIT:
1709 /*
1710 * Initializations with 0 should be allowed. Otherwise,
1711 * we should complain about all uninitialized enums,
1712 * consequently.
1713 */
1714 if (!rn->tn_type->t_isenum && rn->tn_op == CON &&
1715 is_integer(rn->tn_type->t_tspec) &&
1716 rn->tn_val->v_quad == 0) {
1717 return;
1718 }
1719 /* initialisation of '%s' with '%s' */
1720 warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
1721 break;
1722 case FARG:
1723 /* combination of '%s' and '%s', arg #%d */
1724 warning(278,
1725 type_name(ln->tn_type), type_name(rn->tn_type), arg);
1726 break;
1727 case RETURN:
1728 /* combination of '%s' and '%s' in return */
1729 warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
1730 break;
1731 default:
1732 /* combination of '%s' and '%s', op %s */
1733 warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
1734 modtab[op].m_name);
1735 break;
1736 }
1737 }
1738
1739 /*
1740 * Build and initialize a new node.
1741 */
1742 static tnode_t *
1743 new_tnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn)
1744 {
1745 tnode_t *ntn;
1746 tspec_t t;
1747 #ifdef notyet
1748 size_t l;
1749 uint64_t rnum;
1750 #endif
1751
1752 ntn = getnode();
1753
1754 ntn->tn_op = op;
1755 ntn->tn_type = type;
1756 ntn->tn_left = ln;
1757 ntn->tn_right = rn;
1758
1759 switch (op) {
1760 #ifdef notyet
1761 case SHR:
1762 if (rn->tn_op != CON)
1763 break;
1764 rnum = rn->tn_val->v_quad;
1765 l = tsize(ln->tn_type) / CHAR_SIZE;
1766 t = ln->tn_type->t_tspec;
1767 switch (l) {
1768 case 8:
1769 if (rnum >= 56)
1770 t = UCHAR;
1771 else if (rnum >= 48)
1772 t = USHORT;
1773 else if (rnum >= 32)
1774 t = UINT;
1775 break;
1776 case 4:
1777 if (rnum >= 24)
1778 t = UCHAR;
1779 else if (rnum >= 16)
1780 t = USHORT;
1781 break;
1782 case 2:
1783 if (rnum >= 8)
1784 t = UCHAR;
1785 break;
1786 default:
1787 break;
1788 }
1789 if (t != ln->tn_type->t_tspec)
1790 ntn->tn_type->t_tspec = t;
1791 break;
1792 #endif
1793 case STAR:
1794 case FSEL:
1795 lint_assert(ln->tn_type->t_tspec == PTR);
1796 t = ln->tn_type->t_subt->t_tspec;
1797 if (t != FUNC && t != VOID)
1798 ntn->tn_lvalue = true;
1799 break;
1800 default:
1801 break;
1802 }
1803
1804 return ntn;
1805 }
1806
1807 /*
1808 * Performs usual conversion of operands to (unsigned) int.
1809 *
1810 * If tflag is set or the operand is a function argument with no
1811 * type information (no prototype or variable # of args), convert
1812 * float to double.
1813 */
1814 tnode_t *
1815 promote(op_t op, bool farg, tnode_t *tn)
1816 {
1817 tspec_t t;
1818 type_t *ntp;
1819 u_int len;
1820
1821 t = tn->tn_type->t_tspec;
1822
1823 if (!is_arithmetic(t))
1824 return tn;
1825
1826 if (!tflag) {
1827 /*
1828 * ANSI C requires that the result is always of type INT
1829 * if INT can represent all possible values of the previous
1830 * type.
1831 */
1832 if (tn->tn_type->t_bitfield) {
1833 len = tn->tn_type->t_flen;
1834 if (size(INT) > len) {
1835 t = INT;
1836 } else {
1837 lint_assert(len == size(INT));
1838 if (is_uinteger(t)) {
1839 t = UINT;
1840 } else {
1841 t = INT;
1842 }
1843 }
1844 } else if (t == CHAR || t == UCHAR || t == SCHAR) {
1845 t = (size(CHAR) < size(INT) || t != UCHAR) ?
1846 INT : UINT;
1847 } else if (t == SHORT || t == USHORT) {
1848 t = (size(SHORT) < size(INT) || t == SHORT) ?
1849 INT : UINT;
1850 } else if (t == ENUM) {
1851 t = INT;
1852 } else if (farg && t == FLOAT) {
1853 t = DOUBLE;
1854 }
1855 } else {
1856 /*
1857 * In traditional C, keep unsigned and promote FLOAT
1858 * to DOUBLE.
1859 */
1860 if (t == UCHAR || t == USHORT) {
1861 t = UINT;
1862 } else if (t == CHAR || t == SCHAR || t == SHORT) {
1863 t = INT;
1864 } else if (t == FLOAT) {
1865 t = DOUBLE;
1866 } else if (t == ENUM) {
1867 t = INT;
1868 }
1869 }
1870
1871 if (t != tn->tn_type->t_tspec) {
1872 ntp = tduptyp(tn->tn_type);
1873 ntp->t_tspec = t;
1874 /*
1875 * Keep t_isenum so we are later able to check compatibility
1876 * of enum types.
1877 */
1878 tn = convert(op, 0, ntp, tn);
1879 }
1880
1881 return tn;
1882 }
1883
1884 /*
1885 * Insert conversions which are necessary to give both operands the same
1886 * type. This is done in different ways for traditional C and ANIS C.
1887 */
1888 static void
1889 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
1890 {
1891 tspec_t lt, rt, t;
1892 int i;
1893 bool u;
1894 type_t *ntp;
1895 static tspec_t tl[] = {
1896 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
1897 };
1898
1899 lt = (*lnp)->tn_type->t_tspec;
1900 rt = (*rnp)->tn_type->t_tspec;
1901
1902 if (!is_arithmetic(lt) || !is_arithmetic(rt))
1903 return;
1904
1905 if (!tflag) {
1906 if (lt == rt) {
1907 t = lt;
1908 } else if (lt == LCOMPLEX || rt == LCOMPLEX) {
1909 t = LCOMPLEX;
1910 } else if (lt == DCOMPLEX || rt == DCOMPLEX) {
1911 t = DCOMPLEX;
1912 } else if (lt == COMPLEX || rt == COMPLEX) {
1913 t = COMPLEX;
1914 } else if (lt == FCOMPLEX || rt == FCOMPLEX) {
1915 t = FCOMPLEX;
1916 } else if (lt == LDOUBLE || rt == LDOUBLE) {
1917 t = LDOUBLE;
1918 } else if (lt == DOUBLE || rt == DOUBLE) {
1919 t = DOUBLE;
1920 } else if (lt == FLOAT || rt == FLOAT) {
1921 t = FLOAT;
1922 } else {
1923 /*
1924 * If type A has more bits than type B it should
1925 * be able to hold all possible values of type B.
1926 */
1927 if (size(lt) > size(rt)) {
1928 t = lt;
1929 } else if (size(lt) < size(rt)) {
1930 t = rt;
1931 } else {
1932 for (i = 3; tl[i] != INT; i++) {
1933 if (tl[i] == lt || tl[i] == rt)
1934 break;
1935 }
1936 if ((is_uinteger(lt) || is_uinteger(rt)) &&
1937 !is_uinteger(tl[i])) {
1938 i--;
1939 }
1940 t = tl[i];
1941 }
1942 }
1943 } else {
1944 /* Keep unsigned in traditional C */
1945 u = is_uinteger(lt) || is_uinteger(rt);
1946 for (i = 0; tl[i] != INT; i++) {
1947 if (lt == tl[i] || rt == tl[i])
1948 break;
1949 }
1950 t = tl[i];
1951 if (u && is_integer(t) && !is_uinteger(t))
1952 t = unsigned_type(t);
1953 }
1954
1955 if (t != lt) {
1956 ntp = tduptyp((*lnp)->tn_type);
1957 ntp->t_tspec = t;
1958 *lnp = convert(op, 0, ntp, *lnp);
1959 }
1960 if (t != rt) {
1961 ntp = tduptyp((*rnp)->tn_type);
1962 ntp->t_tspec = t;
1963 *rnp = convert(op, 0, ntp, *rnp);
1964 }
1965 }
1966
1967 /*
1968 * Insert a conversion operator, which converts the type of the node
1969 * to another given type.
1970 * If op is FARG, arg is the number of the argument (used for warnings).
1971 */
1972 tnode_t *
1973 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
1974 {
1975 tnode_t *ntn;
1976 tspec_t nt, ot, ost = NOTSPEC;
1977
1978 nt = tp->t_tspec;
1979 if ((ot = tn->tn_type->t_tspec) == PTR)
1980 ost = tn->tn_type->t_subt->t_tspec;
1981
1982 if (!tflag && !sflag && op == FARG)
1983 check_prototype_conversion(arg, nt, ot, tp, tn);
1984 if (is_integer(nt) && is_integer(ot)) {
1985 check_integer_conversion(op, arg, nt, ot, tp, tn);
1986 } else if (nt == PTR && ((ot == PTR && ost == VOID) ||
1987 is_integer(ot)) && tn->tn_op == CON &&
1988 tn->tn_val->v_quad == 0) {
1989 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1990 } else if (is_integer(nt) && nt != BOOL && ot == PTR) {
1991 check_pointer_integer_conversion(op, nt, tp, tn);
1992 } else if (nt == PTR && ot == PTR) {
1993 check_pointer_conversion(op, tn, tp);
1994 }
1995
1996 ntn = getnode();
1997 ntn->tn_op = CVT;
1998 ntn->tn_type = tp;
1999 ntn->tn_cast = op == CVT;
2000 ntn->tn_right = NULL;
2001 if (tn->tn_op != CON || nt == VOID) {
2002 ntn->tn_left = tn;
2003 } else {
2004 ntn->tn_op = CON;
2005 ntn->tn_val = tgetblk(sizeof (val_t));
2006 convert_constant(op, arg, ntn->tn_type, ntn->tn_val,
2007 tn->tn_val);
2008 }
2009
2010 return ntn;
2011 }
2012
2013 /*
2014 * Print a warning if a prototype causes a type conversion that is
2015 * different from what would happen to the same argument in the
2016 * absence of a prototype.
2017 *
2018 * Errors/Warnings about illegal type combinations are already printed
2019 * in check_assign_types_compatible().
2020 */
2021 static void
2022 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
2023 tnode_t *tn)
2024 {
2025 tnode_t *ptn;
2026
2027 if (!is_arithmetic(nt) || !is_arithmetic(ot))
2028 return;
2029
2030 /*
2031 * If the type of the formal parameter is char/short, a warning
2032 * would be useless, because functions declared the old style
2033 * can't expect char/short arguments.
2034 */
2035 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
2036 return;
2037
2038 /* get default promotion */
2039 ptn = promote(NOOP, 1, tn);
2040 ot = ptn->tn_type->t_tspec;
2041
2042 /* return if types are the same with and without prototype */
2043 if (nt == ot || (nt == ENUM && ot == INT))
2044 return;
2045
2046 if (is_floating(nt) != is_floating(ot) ||
2047 psize(nt) != psize(ot)) {
2048 /* representation and/or width change */
2049 if (!is_integer(ot) || psize(ot) > psize(INT)) {
2050 /* conversion to '%s' due to prototype, arg #%d */
2051 warning(259, type_name(tp), arg);
2052 }
2053 } else if (hflag) {
2054 /*
2055 * they differ in sign or base type (char, short, int,
2056 * long, long long, float, double, long double)
2057 *
2058 * if they differ only in sign and the argument is a constant
2059 * and the msb of the argument is not set, print no warning
2060 */
2061 if (ptn->tn_op == CON && is_integer(nt) &&
2062 signed_type(nt) == signed_type(ot) &&
2063 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
2064 /* ok */
2065 } else {
2066 /* conversion to '%s' due to prototype, arg #%d */
2067 warning(259, type_name(tp), arg);
2068 }
2069 }
2070 }
2071
2072 /*
2073 * Print warnings for conversions of integer types which may cause problems.
2074 */
2075 /* ARGSUSED */
2076 static void
2077 check_integer_conversion(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp,
2078 tnode_t *tn)
2079 {
2080 char opbuf[16];
2081
2082 if (tn->tn_op == CON)
2083 return;
2084
2085 if (op == CVT)
2086 return;
2087
2088 if (Pflag && psize(nt) > psize(ot) &&
2089 is_uinteger(nt) != is_uinteger(ot)) {
2090 if (aflag > 0 && pflag) {
2091 if (op == FARG) {
2092 /* conversion to '%s' may sign-extend ... */
2093 warning(297, type_name(tp), arg);
2094 } else {
2095 /* conversion to '%s' may sign-extend ... */
2096 warning(131, type_name(tp));
2097 }
2098 }
2099 }
2100
2101 if (Pflag && psize(nt) > psize(ot)) {
2102 switch (tn->tn_op) {
2103 case PLUS:
2104 case MINUS:
2105 case MULT:
2106 case SHL:
2107 /* suggest cast from '%s' to '%s' on op %s to ... */
2108 warning(324, type_name(gettyp(ot)), type_name(tp),
2109 print_tnode(opbuf, sizeof(opbuf), tn));
2110 break;
2111 default:
2112 break;
2113 }
2114 }
2115
2116 if (psize(nt) < psize(ot) &&
2117 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
2118 aflag > 1)) {
2119 /* conversion from '%s' may lose accuracy */
2120 if (aflag > 0) {
2121 if (op == FARG) {
2122 /* conv. from '%s' to '%s' may lose ... */
2123 warning(298,
2124 type_name(tn->tn_type), type_name(tp), arg);
2125 } else {
2126 /* conv. from '%s' to '%s' may lose accuracy */
2127 warning(132,
2128 type_name(tn->tn_type), type_name(tp));
2129 }
2130 }
2131 }
2132 }
2133
2134 /*
2135 * Print warnings for dubious conversions of pointer to integer.
2136 */
2137 static void
2138 check_pointer_integer_conversion(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
2139 {
2140
2141 if (tn->tn_op == CON)
2142 return;
2143
2144 if (op != CVT) {
2145 /* We got already an error. */
2146 return;
2147 }
2148
2149 if (psize(nt) < psize(PTR)) {
2150 if (pflag && size(nt) >= size(PTR)) {
2151 /* conversion of pointer to '%s' may lose bits */
2152 warning(134, type_name(tp));
2153 } else {
2154 /* conversion of pointer to '%s' loses bits */
2155 warning(133, type_name(tp));
2156 }
2157 }
2158 }
2159
2160 /*
2161 * Print warnings for questionable pointer conversions.
2162 */
2163 static void
2164 check_pointer_conversion(op_t op, tnode_t *tn, type_t *tp)
2165 {
2166 tspec_t nt, ot;
2167 const char *nts, *ots;
2168
2169 /*
2170 * We got already an error (pointers of different types
2171 * without a cast) or we will not get a warning.
2172 */
2173 if (op != CVT)
2174 return;
2175
2176 nt = tp->t_subt->t_tspec;
2177 ot = tn->tn_type->t_subt->t_tspec;
2178
2179 if (nt == VOID || ot == VOID) {
2180 if (sflag && (nt == FUNC || ot == FUNC)) {
2181 /* (void *)0 already handled in convert() */
2182 *(nt == FUNC ? &nts : &ots) = "function pointer";
2183 *(nt == VOID ? &nts : &ots) = "'void *'";
2184 /* ANSI C forbids conversion of %s to %s */
2185 warning(303, ots, nts);
2186 }
2187 return;
2188 } else if (nt == FUNC && ot == FUNC) {
2189 return;
2190 } else if (nt == FUNC || ot == FUNC) {
2191 /* questionable conversion of function pointer */
2192 warning(229);
2193 return;
2194 }
2195
2196 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
2197 if (hflag)
2198 /* possible pointer alignment problem */
2199 warning(135);
2200 }
2201 if (((nt == STRUCT || nt == UNION) &&
2202 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
2203 psize(nt) != psize(ot)) {
2204 if (cflag) {
2205 /* pointer casts may be troublesome */
2206 warning(247);
2207 }
2208 }
2209 }
2210
2211 /*
2212 * Converts a typed constant in a constant of another type.
2213 *
2214 * op operator which requires conversion
2215 * arg if op is FARG, # of argument
2216 * tp type in which to convert the constant
2217 * nv new constant
2218 * v old constant
2219 */
2220 void
2221 convert_constant(op_t op, int arg, type_t *tp, val_t *nv, val_t *v)
2222 {
2223 tspec_t ot, nt;
2224 ldbl_t max = 0.0, min = 0.0;
2225 int sz;
2226 bool rchk;
2227 int64_t xmask, xmsk1;
2228 int osz, nsz;
2229
2230 ot = v->v_tspec;
2231 nt = nv->v_tspec = tp->t_tspec;
2232 rchk = false;
2233
2234 if (nt == BOOL) { /* C99 6.3.1.2 */
2235 nv->v_ansiu = false;
2236 nv->v_quad = is_nonzero_val(ot, v) ? 1 : 0;
2237 return;
2238 }
2239
2240 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
2241 switch (nt) {
2242 case CHAR:
2243 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
2244 case UCHAR:
2245 max = TARG_UCHAR_MAX; min = 0; break;
2246 case SCHAR:
2247 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
2248 case SHORT:
2249 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
2250 case USHORT:
2251 max = TARG_USHRT_MAX; min = 0; break;
2252 case ENUM:
2253 case INT:
2254 max = TARG_INT_MAX; min = TARG_INT_MIN; break;
2255 case UINT:
2256 max = (u_int)TARG_UINT_MAX;min = 0; break;
2257 case LONG:
2258 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
2259 case ULONG:
2260 max = (u_long)TARG_ULONG_MAX; min = 0; break;
2261 case QUAD:
2262 max = QUAD_MAX; min = QUAD_MIN; break;
2263 case UQUAD:
2264 max = (uint64_t)UQUAD_MAX; min = 0; break;
2265 case FLOAT:
2266 case FCOMPLEX:
2267 max = FLT_MAX; min = -FLT_MAX; break;
2268 case DOUBLE:
2269 case DCOMPLEX:
2270 max = DBL_MAX; min = -DBL_MAX; break;
2271 case PTR:
2272 /* Got already an error because of float --> ptr */
2273 case LDOUBLE:
2274 case LCOMPLEX:
2275 max = LDBL_MAX; min = -LDBL_MAX; break;
2276 default:
2277 lint_assert(/*CONSTCOND*/false);
2278 }
2279 if (v->v_ldbl > max || v->v_ldbl < min) {
2280 lint_assert(nt != LDOUBLE);
2281 if (op == FARG) {
2282 /* conv. of '%s' to '%s' is out of range, ... */
2283 warning(295,
2284 type_name(gettyp(ot)), type_name(tp), arg);
2285 } else {
2286 /* conversion of '%s' to '%s' is out of range */
2287 warning(119,
2288 type_name(gettyp(ot)), type_name(tp));
2289 }
2290 v->v_ldbl = v->v_ldbl > 0 ? max : min;
2291 }
2292 if (nt == FLOAT) {
2293 nv->v_ldbl = (float)v->v_ldbl;
2294 } else if (nt == DOUBLE) {
2295 nv->v_ldbl = (double)v->v_ldbl;
2296 } else if (nt == LDOUBLE) {
2297 nv->v_ldbl = v->v_ldbl;
2298 } else {
2299 nv->v_quad = (nt == PTR || is_uinteger(nt)) ?
2300 (int64_t)v->v_ldbl : (int64_t)v->v_ldbl;
2301 }
2302 } else {
2303 if (nt == FLOAT) {
2304 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2305 (float)(uint64_t)v->v_quad : (float)v->v_quad;
2306 } else if (nt == DOUBLE) {
2307 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2308 (double)(uint64_t)v->v_quad : (double)v->v_quad;
2309 } else if (nt == LDOUBLE) {
2310 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2311 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
2312 } else {
2313 rchk = true; /* Check for lost precision. */
2314 nv->v_quad = v->v_quad;
2315 }
2316 }
2317
2318 if (v->v_ansiu && is_floating(nt)) {
2319 /* ANSI C treats constant as unsigned */
2320 warning(157);
2321 v->v_ansiu = false;
2322 } else if (v->v_ansiu && (is_integer(nt) && !is_uinteger(nt) &&
2323 psize(nt) > psize(ot))) {
2324 /* ANSI C treats constant as unsigned */
2325 warning(157);
2326 v->v_ansiu = false;
2327 }
2328
2329 switch (nt) {
2330 case FLOAT:
2331 case FCOMPLEX:
2332 case DOUBLE:
2333 case DCOMPLEX:
2334 case LDOUBLE:
2335 case LCOMPLEX:
2336 break;
2337 default:
2338 sz = tp->t_bitfield ? tp->t_flen : size(nt);
2339 nv->v_quad = xsign(nv->v_quad, nt, sz);
2340 break;
2341 }
2342
2343 if (rchk && op != CVT) {
2344 osz = size(ot);
2345 nsz = tp->t_bitfield ? tp->t_flen : size(nt);
2346 xmask = qlmasks[nsz] ^ qlmasks[osz];
2347 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
2348 /*
2349 * For bitwise operations we are not interested in the
2350 * value, but in the bits itself.
2351 */
2352 if (op == ORASS || op == OR || op == XOR) {
2353 /*
2354 * Print a warning if bits which were set are
2355 * lost due to the conversion.
2356 * This can happen with operator ORASS only.
2357 */
2358 if (nsz < osz && (v->v_quad & xmask) != 0) {
2359 /* constant truncated by conv., op %s */
2360 warning(306, modtab[op].m_name);
2361 }
2362 } else if (op == ANDASS || op == AND) {
2363 /*
2364 * Print a warning if additional bits are not all 1
2365 * and the most significant bit of the old value is 1,
2366 * or if at least one (but not all) removed bit was 0.
2367 */
2368 if (nsz > osz &&
2369 (nv->v_quad & qbmasks[osz - 1]) != 0 &&
2370 (nv->v_quad & xmask) != xmask) {
2371 /* extra bits set to 0 in conv. of '%s' ... */
2372 warning(309, type_name(gettyp(ot)),
2373 type_name(tp), modtab[op].m_name);
2374 } else if (nsz < osz &&
2375 (v->v_quad & xmask) != xmask &&
2376 (v->v_quad & xmask) != 0) {
2377 /* constant truncated by conv., op %s */
2378 warning(306, modtab[op].m_name);
2379 }
2380 } else if ((nt != PTR && is_uinteger(nt)) &&
2381 (ot != PTR && !is_uinteger(ot)) &&
2382 v->v_quad < 0) {
2383 if (op == ASSIGN) {
2384 /* assignment of negative constant to ... */
2385 warning(164);
2386 } else if (op == INIT) {
2387 /* initialisation of unsigned with neg... */
2388 warning(221);
2389 } else if (op == FARG) {
2390 /* conversion of negative constant to ... */
2391 warning(296, arg);
2392 } else if (modtab[op].m_comp) {
2393 /* handled by check_integer_comparison() */
2394 } else {
2395 /* conversion of negative constant to ... */
2396 warning(222);
2397 }
2398 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2399 (v->v_quad & xmask) != 0 &&
2400 (is_uinteger(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2401 /*
2402 * Loss of significant bit(s). All truncated bits
2403 * of unsigned types or all truncated bits plus the
2404 * msb of the target for signed types are considered
2405 * to be significant bits. Loss of significant bits
2406 * means that at least on of the bits was set in an
2407 * unsigned type or that at least one, but not all of
2408 * the bits was set in an signed type.
2409 * Loss of significant bits means that it is not
2410 * possible, also not with necessary casts, to convert
2411 * back to the original type. A example for a
2412 * necessary cast is:
2413 * char c; int i; c = 128;
2414 * i = c; ** yields -128 **
2415 * i = (unsigned char)c; ** yields 128 **
2416 */
2417 if (op == ASSIGN && tp->t_bitfield) {
2418 /* precision lost in bit-field assignment */
2419 warning(166);
2420 } else if (op == ASSIGN) {
2421 /* constant truncated by assignment */
2422 warning(165);
2423 } else if (op == INIT && tp->t_bitfield) {
2424 /* bit-field initializer does not fit */
2425 warning(180);
2426 } else if (op == INIT) {
2427 /* initializer does not fit */
2428 warning(178);
2429 } else if (op == CASE) {
2430 /* case label affected by conversion */
2431 warning(196);
2432 } else if (op == FARG) {
2433 /* conv. of '%s' to '%s' is out of range, ... */
2434 warning(295,
2435 type_name(gettyp(ot)), type_name(tp), arg);
2436 } else {
2437 /* conversion of '%s' to '%s' is out of range */
2438 warning(119,
2439 type_name(gettyp(ot)), type_name(tp));
2440 }
2441 } else if (nv->v_quad != v->v_quad) {
2442 if (op == ASSIGN && tp->t_bitfield) {
2443 /* precision lost in bit-field assignment */
2444 warning(166);
2445 } else if (op == INIT && tp->t_bitfield) {
2446 /* bit-field initializer out of range */
2447 warning(11);
2448 } else if (op == CASE) {
2449 /* case label affected by conversion */
2450 warning(196);
2451 } else if (op == FARG) {
2452 /* conv. of '%s' to '%s' is out of range, ... */
2453 warning(295,
2454 type_name(gettyp(ot)), type_name(tp), arg);
2455 } else {
2456 /* conversion of '%s' to '%s' is out of range */
2457 warning(119,
2458 type_name(gettyp(ot)), type_name(tp));
2459 }
2460 }
2461 }
2462 }
2463
2464 /*
2465 * Called if incompatible types were detected.
2466 * Prints a appropriate warning.
2467 */
2468 static void
2469 warn_incompatible_types(op_t op, tspec_t lt, tspec_t rt)
2470 {
2471 mod_t *mp;
2472
2473 mp = &modtab[op];
2474
2475 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2476 /* void type illegal in expression */
2477 error(109);
2478 } else if (op == ASSIGN) {
2479 if ((lt == STRUCT || lt == UNION) &&
2480 (rt == STRUCT || rt == UNION)) {
2481 /* assignment of different structures (%s != %s) */
2482 error(240, tspec_name(lt), tspec_name(rt));
2483 } else {
2484 /* assignment type mismatch (%s != %s) */
2485 error(171, tspec_name(lt), tspec_name(rt));
2486 }
2487 } else if (mp->m_binary) {
2488 /* operands of '%s' have incompatible types (%s != %s) */
2489 error(107, mp->m_name, tspec_name(lt), tspec_name(rt));
2490 } else {
2491 lint_assert(rt == NOTSPEC);
2492 /* operand of '%s' has invalid type (%s) */
2493 error(108, mp->m_name, tspec_name(lt));
2494 }
2495 }
2496
2497 /*
2498 * Called if incompatible pointer types are detected.
2499 * Print an appropriate warning.
2500 */
2501 static void
2502 warn_incompatible_pointers(const mod_t *mp,
2503 const type_t *ltp, const type_t *rtp)
2504 {
2505 tspec_t lt, rt;
2506
2507 lint_assert(ltp->t_tspec == PTR);
2508 lint_assert(rtp->t_tspec == PTR);
2509
2510 lt = ltp->t_subt->t_tspec;
2511 rt = rtp->t_subt->t_tspec;
2512
2513 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2514 if (mp == NULL) {
2515 /* illegal structure pointer combination */
2516 warning(244);
2517 } else {
2518 /* illegal structure pointer combination, op %s */
2519 warning(245, mp->m_name);
2520 }
2521 } else {
2522 if (mp == NULL) {
2523 /* illegal pointer combination */
2524 warning(184);
2525 } else {
2526 /* illegal pointer combination (%s) and (%s), op %s */
2527 warning(124,
2528 type_name(ltp), type_name(rtp), mp->m_name);
2529 }
2530 }
2531 }
2532
2533 /*
2534 * Make sure type (*tpp)->t_subt has at least the qualifiers
2535 * of tp1->t_subt and tp2->t_subt.
2536 */
2537 static void
2538 merge_qualifiers(type_t **tpp, type_t *tp1, type_t *tp2)
2539 {
2540
2541 lint_assert((*tpp)->t_tspec == PTR);
2542 lint_assert(tp1->t_tspec == PTR);
2543 lint_assert(tp2->t_tspec == PTR);
2544
2545 if ((*tpp)->t_subt->t_const ==
2546 (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
2547 (*tpp)->t_subt->t_volatile ==
2548 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
2549 return;
2550 }
2551
2552 *tpp = tduptyp(*tpp);
2553 (*tpp)->t_subt = tduptyp((*tpp)->t_subt);
2554 (*tpp)->t_subt->t_const =
2555 tp1->t_subt->t_const | tp2->t_subt->t_const;
2556 (*tpp)->t_subt->t_volatile =
2557 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
2558 }
2559
2560 /*
2561 * Returns 1 if the given structure or union has a constant member
2562 * (maybe recursively).
2563 */
2564 static bool
2565 has_constant_member(const type_t *tp)
2566 {
2567 sym_t *m;
2568 tspec_t t;
2569
2570 lint_assert((t = tp->t_tspec) == STRUCT || t == UNION);
2571
2572 for (m = tp->t_str->memb; m != NULL; m = m->s_next) {
2573 tp = m->s_type;
2574 if (tp->t_const)
2575 return true;
2576 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
2577 if (has_constant_member(m->s_type))
2578 return true;
2579 }
2580 }
2581 return false;
2582 }
2583
2584 /*
2585 * Create a new node for one of the operators POINT and ARROW.
2586 */
2587 static tnode_t *
2588 build_struct_access(op_t op, tnode_t *ln, tnode_t *rn)
2589 {
2590 tnode_t *ntn, *ctn;
2591 bool nolval;
2592
2593 lint_assert(rn->tn_op == NAME);
2594 lint_assert(rn->tn_sym->s_value.v_tspec == INT);
2595 lint_assert(rn->tn_sym->s_scl == MOS || rn->tn_sym->s_scl == MOU);
2596
2597 /*
2598 * Remember if the left operand is an lvalue (structure members
2599 * are lvalues if and only if the structure itself is an lvalue).
2600 */
2601 nolval = op == POINT && !ln->tn_lvalue;
2602
2603 if (op == POINT) {
2604 ln = build_ampersand(ln, 1);
2605 } else if (ln->tn_type->t_tspec != PTR) {
2606 lint_assert(tflag);
2607 lint_assert(is_integer(ln->tn_type->t_tspec));
2608 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
2609 }
2610
2611 #if PTRDIFF_IS_LONG
2612 ctn = new_integer_constant_node(LONG,
2613 rn->tn_sym->s_value.v_quad / CHAR_SIZE);
2614 #else
2615 ctn = new_integer_constant_node(INT,
2616 rn->tn_sym->s_value.v_quad / CHAR_SIZE);
2617 #endif
2618
2619 ntn = new_tnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
2620 if (ln->tn_op == CON)
2621 ntn = fold(ntn);
2622
2623 if (rn->tn_type->t_bitfield) {
2624 ntn = new_tnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2625 } else {
2626 ntn = new_tnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
2627 }
2628
2629 if (nolval)
2630 ntn->tn_lvalue = false;
2631
2632 return ntn;
2633 }
2634
2635 /*
2636 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2637 */
2638 static tnode_t *
2639 build_prepost_incdec(op_t op, tnode_t *ln)
2640 {
2641 tnode_t *cn, *ntn;
2642
2643 lint_assert(ln != NULL);
2644
2645 if (ln->tn_type->t_tspec == PTR) {
2646 cn = plength(ln->tn_type);
2647 } else {
2648 cn = new_integer_constant_node(INT, (int64_t)1);
2649 }
2650 ntn = new_tnode(op, ln->tn_type, ln, cn);
2651
2652 return ntn;
2653 }
2654
2655 /*
2656 * Create a node for REAL, IMAG
2657 */
2658 static tnode_t *
2659 build_real_imag(op_t op, tnode_t *ln)
2660 {
2661 tnode_t *cn, *ntn;
2662
2663 lint_assert(ln != NULL);
2664
2665 switch (ln->tn_type->t_tspec) {
2666 case LCOMPLEX:
2667 cn = new_integer_constant_node(LDOUBLE, (int64_t)1);
2668 break;
2669 case DCOMPLEX:
2670 cn = new_integer_constant_node(DOUBLE, (int64_t)1);
2671 break;
2672 case FCOMPLEX:
2673 cn = new_integer_constant_node(FLOAT, (int64_t)1);
2674 break;
2675 default:
2676 /* __%s__ is illegal for type %s */
2677 error(276, op == REAL ? "real" : "imag",
2678 type_name(ln->tn_type));
2679 return NULL;
2680 }
2681 ntn = new_tnode(op, cn->tn_type, ln, cn);
2682 ntn->tn_lvalue = true;
2683
2684 return ntn;
2685 }
2686 /*
2687 * Create a tree node for the & operator
2688 */
2689 static tnode_t *
2690 build_ampersand(tnode_t *tn, bool noign)
2691 {
2692 tnode_t *ntn;
2693 tspec_t t;
2694
2695 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2696 if (tflag)
2697 /* '&' before array or function: ignored */
2698 warning(127);
2699 return tn;
2700 }
2701
2702 /* eliminate &* */
2703 if (tn->tn_op == STAR &&
2704 tn->tn_left->tn_type->t_tspec == PTR &&
2705 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2706 return tn->tn_left;
2707 }
2708
2709 ntn = new_tnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
2710
2711 return ntn;
2712 }
2713
2714 /*
2715 * Create a node for operators PLUS and MINUS.
2716 */
2717 static tnode_t *
2718 build_plus_minus(op_t op, tnode_t *ln, tnode_t *rn)
2719 {
2720 tnode_t *ntn, *ctn;
2721 type_t *tp;
2722
2723 /* If pointer and integer, then pointer to the lhs. */
2724 if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
2725 ntn = ln;
2726 ln = rn;
2727 rn = ntn;
2728 }
2729
2730 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2731
2732 lint_assert(is_integer(rn->tn_type->t_tspec));
2733
2734 ctn = plength(ln->tn_type);
2735 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2736 rn = convert(NOOP, 0, ctn->tn_type, rn);
2737 rn = new_tnode(MULT, rn->tn_type, rn, ctn);
2738 if (rn->tn_left->tn_op == CON)
2739 rn = fold(rn);
2740 ntn = new_tnode(op, ln->tn_type, ln, rn);
2741
2742 } else if (rn->tn_type->t_tspec == PTR) {
2743
2744 lint_assert(ln->tn_type->t_tspec == PTR);
2745 lint_assert(op == MINUS);
2746 #if PTRDIFF_IS_LONG
2747 tp = gettyp(LONG);
2748 #else
2749 tp = gettyp(INT);
2750 #endif
2751 ntn = new_tnode(op, tp, ln, rn);
2752 if (ln->tn_op == CON && rn->tn_op == CON)
2753 ntn = fold(ntn);
2754 ctn = plength(ln->tn_type);
2755 balance(NOOP, &ntn, &ctn);
2756 ntn = new_tnode(DIV, tp, ntn, ctn);
2757
2758 } else {
2759
2760 ntn = new_tnode(op, ln->tn_type, ln, rn);
2761
2762 }
2763 return ntn;
2764 }
2765
2766 /*
2767 * Create a node for operators SHL and SHR.
2768 */
2769 static tnode_t *
2770 build_bit_shift(op_t op, tnode_t *ln, tnode_t *rn)
2771 {
2772 tspec_t t;
2773 tnode_t *ntn;
2774
2775 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2776 rn = convert(CVT, 0, gettyp(INT), rn);
2777 ntn = new_tnode(op, ln->tn_type, ln, rn);
2778 return ntn;
2779 }
2780
2781 /*
2782 * Create a node for COLON.
2783 */
2784 static tnode_t *
2785 build_colon(tnode_t *ln, tnode_t *rn)
2786 {
2787 tspec_t lt, rt, pdt;
2788 type_t *rtp;
2789 tnode_t *ntn;
2790
2791 lt = ln->tn_type->t_tspec;
2792 rt = rn->tn_type->t_tspec;
2793 #if PTRDIFF_IS_LONG
2794 pdt = LONG;
2795 #else
2796 pdt = INT;
2797 #endif
2798
2799 /*
2800 * Arithmetic types are balanced, all other type combinations
2801 * still need to be handled.
2802 */
2803 if (is_arithmetic(lt) && is_arithmetic(rt)) {
2804 rtp = ln->tn_type;
2805 } else if (lt == VOID || rt == VOID) {
2806 rtp = gettyp(VOID);
2807 } else if (lt == STRUCT || lt == UNION) {
2808 /* Both types must be identical. */
2809 lint_assert(rt == STRUCT || rt == UNION);
2810 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
2811 if (incompl(ln->tn_type)) {
2812 /* unknown operand size, op %s */
2813 error(138, modtab[COLON].m_name);
2814 return NULL;
2815 }
2816 rtp = ln->tn_type;
2817 } else if (lt == PTR && is_integer(rt)) {
2818 if (rt != pdt) {
2819 rn = convert(NOOP, 0, gettyp(pdt), rn);
2820 rt = pdt;
2821 }
2822 rtp = ln->tn_type;
2823 } else if (rt == PTR && is_integer(lt)) {
2824 if (lt != pdt) {
2825 ln = convert(NOOP, 0, gettyp(pdt), ln);
2826 lt = pdt;
2827 }
2828 rtp = rn->tn_type;
2829 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2830 lint_assert(rt == PTR);
2831 rtp = rn->tn_type;
2832 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
2833 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2834 lint_assert(lt == PTR);
2835 rtp = ln->tn_type;
2836 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
2837 } else {
2838 lint_assert(lt == PTR);
2839 lint_assert(rt == PTR);
2840 /*
2841 * XXX For now we simply take the left type. This is
2842 * probably wrong, if one type contains a function prototype
2843 * and the other one, at the same place, only an old style
2844 * declaration.
2845 */
2846 rtp = ln->tn_type;
2847 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
2848 }
2849
2850 ntn = new_tnode(COLON, rtp, ln, rn);
2851
2852 return ntn;
2853 }
2854
2855 /*
2856 * Create a node for an assignment operator (both = and op= ).
2857 */
2858 static tnode_t *
2859 build_assignment(op_t op, tnode_t *ln, tnode_t *rn)
2860 {
2861 tspec_t lt, rt;
2862 tnode_t *ntn, *ctn;
2863
2864 lint_assert(ln != NULL);
2865 lint_assert(rn != NULL);
2866
2867 lt = ln->tn_type->t_tspec;
2868 rt = rn->tn_type->t_tspec;
2869
2870 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2871 lint_assert(is_integer(rt));
2872 ctn = plength(ln->tn_type);
2873 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2874 rn = convert(NOOP, 0, ctn->tn_type, rn);
2875 rn = new_tnode(MULT, rn->tn_type, rn, ctn);
2876 if (rn->tn_left->tn_op == CON)
2877 rn = fold(rn);
2878 }
2879
2880 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
2881 lint_assert(lt == rt);
2882 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
2883 if (incompl(ln->tn_type)) {
2884 if (op == RETURN) {
2885 /* cannot return incomplete type */
2886 error(212);
2887 } else {
2888 /* unknown operand size, op %s */
2889 error(138, modtab[op].m_name);
2890 }
2891 return NULL;
2892 }
2893 }
2894
2895 if (op == SHLASS) {
2896 if (psize(lt) < psize(rt)) {
2897 if (hflag)
2898 /* semantics of '%s' change in ANSI C; ... */
2899 warning(118, "<<=");
2900 }
2901 } else if (op != SHRASS) {
2902 if (op == ASSIGN || lt != PTR) {
2903 if (lt != rt ||
2904 (ln->tn_type->t_bitfield && rn->tn_op == CON)) {
2905 rn = convert(op, 0, ln->tn_type, rn);
2906 rt = lt;
2907 }
2908 }
2909 }
2910
2911 ntn = new_tnode(op, ln->tn_type, ln, rn);
2912
2913 return ntn;
2914 }
2915
2916 /*
2917 * Get length of type tp->t_subt.
2918 */
2919 static tnode_t *
2920 plength(type_t *tp)
2921 {
2922 int elem, elsz;
2923 tspec_t st;
2924
2925 lint_assert(tp->t_tspec == PTR);
2926 tp = tp->t_subt;
2927
2928 elem = 1;
2929 elsz = 0;
2930
2931 while (tp->t_tspec == ARRAY) {
2932 elem *= tp->t_dim;
2933 tp = tp->t_subt;
2934 }
2935
2936 switch (tp->t_tspec) {
2937 case FUNC:
2938 /* pointer to function is not allowed here */
2939 error(110);
2940 break;
2941 case VOID:
2942 /* cannot do pointer arithmetic on operand of ... */
2943 gnuism(136);
2944 break;
2945 case STRUCT:
2946 case UNION:
2947 if ((elsz = tp->t_str->size) == 0)
2948 /* cannot do pointer arithmetic on operand of ... */
2949 error(136);
2950 break;
2951 case ENUM:
2952 if (incompl(tp)) {
2953 /* cannot do pointer arithmetic on operand of ... */
2954 warning(136);
2955 }
2956 /* FALLTHROUGH */
2957 default:
2958 if ((elsz = size(tp->t_tspec)) == 0) {
2959 /* cannot do pointer arithmetic on operand of ... */
2960 error(136);
2961 } else {
2962 lint_assert(elsz != -1);
2963 }
2964 break;
2965 }
2966
2967 if (elem == 0 && elsz != 0) {
2968 /* cannot do pointer arithmetic on operand of ... */
2969 error(136);
2970 }
2971
2972 if (elsz == 0)
2973 elsz = CHAR_SIZE;
2974
2975 #if PTRDIFF_IS_LONG
2976 st = LONG;
2977 #else
2978 st = INT;
2979 #endif
2980
2981 return new_integer_constant_node(st, (int64_t)(elem * elsz / CHAR_SIZE));
2982 }
2983
2984 /*
2985 * XXX
2986 * Note: There appear to be a number of bugs in detecting overflow in
2987 * this function. An audit and a set of proper regression tests are needed.
2988 * --Perry Metzger, Nov. 16, 2001
2989 */
2990 /*
2991 * Do only as much as necessary to compute constant expressions.
2992 * Called only if the operator allows folding and (both) operands
2993 * are constants.
2994 */
2995 static tnode_t *
2996 fold(tnode_t *tn)
2997 {
2998 val_t *v;
2999 tspec_t t;
3000 bool utyp, ovfl;
3001 int64_t sl, sr = 0, q = 0, mask;
3002 uint64_t ul, ur = 0;
3003 tnode_t *cn;
3004
3005 v = xcalloc(1, sizeof (val_t));
3006 v->v_tspec = t = tn->tn_type->t_tspec;
3007
3008 utyp = t == PTR || is_uinteger(t);
3009 ul = sl = tn->tn_left->tn_val->v_quad;
3010 if (modtab[tn->tn_op].m_binary)
3011 ur = sr = tn->tn_right->tn_val->v_quad;
3012
3013 mask = qlmasks[size(t)];
3014 ovfl = false;
3015
3016 switch (tn->tn_op) {
3017 case UPLUS:
3018 q = sl;
3019 break;
3020 case UMINUS:
3021 q = -sl;
3022 if (sl != 0 && msb(q, t, -1) == msb(sl, t, -1))
3023 ovfl = true;
3024 break;
3025 case COMPL:
3026 q = ~sl;
3027 break;
3028 case MULT:
3029 if (utyp) {
3030 q = ul * ur;
3031 if (q != (q & mask))
3032 ovfl = true;
3033 else if ((ul != 0) && ((q / ul) != ur))
3034 ovfl = true;
3035 } else {
3036 q = sl * sr;
3037 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
3038 ovfl = true;
3039 }
3040 break;
3041 case DIV:
3042 if (sr == 0) {
3043 /* division by 0 */
3044 error(139);
3045 q = utyp ? UQUAD_MAX : QUAD_MAX;
3046 } else {
3047 q = utyp ? (int64_t)(ul / ur) : sl / sr;
3048 }
3049 break;
3050 case MOD:
3051 if (sr == 0) {
3052 /* modulus by 0 */
3053 error(140);
3054 q = 0;
3055 } else {
3056 q = utyp ? (int64_t)(ul % ur) : sl % sr;
3057 }
3058 break;
3059 case PLUS:
3060 q = utyp ? (int64_t)(ul + ur) : sl + sr;
3061 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) {
3062 if (msb(q, t, -1) == 0)
3063 ovfl = true;
3064 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
3065 if (msb(q, t, -1) != 0)
3066 ovfl = true;
3067 }
3068 break;
3069 case MINUS:
3070 q = utyp ? (int64_t)(ul - ur) : sl - sr;
3071 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
3072 if (msb(q, t, -1) == 0)
3073 ovfl = true;
3074 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
3075 if (msb(q, t, -1) != 0)
3076 ovfl = true;
3077 }
3078 break;
3079 case SHL:
3080 q = utyp ? (int64_t)(ul << sr) : sl << sr;
3081 break;
3082 case SHR:
3083 /*
3084 * The sign must be explicitly extended because
3085 * shifts of signed values are implementation dependent.
3086 */
3087 q = ul >> sr;
3088 q = xsign(q, t, size(t) - (int)sr);
3089 break;
3090 case LT:
3091 q = (utyp ? ul < ur : sl < sr) ? 1 : 0;
3092 break;
3093 case LE:
3094 q = (utyp ? ul <= ur : sl <= sr) ? 1 : 0;
3095 break;
3096 case GE:
3097 q = (utyp ? ul >= ur : sl >= sr) ? 1 : 0;
3098 break;
3099 case GT:
3100 q = (utyp ? ul > ur : sl > sr) ? 1 : 0;
3101 break;
3102 case EQ:
3103 q = (utyp ? ul == ur : sl == sr) ? 1 : 0;
3104 break;
3105 case NE:
3106 q = (utyp ? ul != ur : sl != sr) ? 1 : 0;
3107 break;
3108 case AND:
3109 q = utyp ? (int64_t)(ul & ur) : sl & sr;
3110 break;
3111 case XOR:
3112 q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr;
3113 break;
3114 case OR:
3115 q = utyp ? (int64_t)(ul | ur) : sl | sr;
3116 break;
3117 default:
3118 lint_assert(/*CONSTCOND*/false);
3119 }
3120
3121 /* XXX does not work for quads. */
3122 if (ovfl || ((uint64_t)(q | mask) != ~(uint64_t)0 &&
3123 (q & ~mask) != 0)) {
3124 if (hflag)
3125 /* integer overflow detected, op %s */
3126 warning(141, modtab[tn->tn_op].m_name);
3127 }
3128
3129 v->v_quad = xsign(q, t, -1);
3130
3131 cn = new_constant_node(tn->tn_type, v);
3132
3133 return cn;
3134 }
3135
3136 /*
3137 * Same for operators whose operands are compared with 0 (test context).
3138 */
3139 static tnode_t *
3140 fold_test(tnode_t *tn)
3141 {
3142 bool l, r;
3143 val_t *v;
3144
3145 v = xcalloc(1, sizeof (val_t));
3146 v->v_tspec = tn->tn_type->t_tspec;
3147 lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
3148
3149 l = is_nonzero(tn->tn_left);
3150 r = modtab[tn->tn_op].m_binary && is_nonzero(tn->tn_right);
3151
3152 switch (tn->tn_op) {
3153 case NOT:
3154 if (hflag && !constcond_flag)
3155 /* constant argument to NOT */
3156 warning(239);
3157 v->v_quad = !l ? 1 : 0;
3158 break;
3159 case LOGAND:
3160 v->v_quad = l && r ? 1 : 0;
3161 break;
3162 case LOGOR:
3163 v->v_quad = l || r ? 1 : 0;
3164 break;
3165 default:
3166 lint_assert(/*CONSTCOND*/false);
3167 }
3168
3169 return new_constant_node(tn->tn_type, v);
3170 }
3171
3172 /*
3173 * Same for operands with floating point type.
3174 */
3175 static tnode_t *
3176 fold_float(tnode_t *tn)
3177 {
3178 val_t *v;
3179 tspec_t t;
3180 ldbl_t l, r = 0;
3181
3182 fpe = 0;
3183 v = xcalloc(1, sizeof (val_t));
3184 v->v_tspec = t = tn->tn_type->t_tspec;
3185
3186 lint_assert(is_floating(t));
3187 lint_assert(t == tn->tn_left->tn_type->t_tspec);
3188 lint_assert(!modtab[tn->tn_op].m_binary ||
3189 t == tn->tn_right->tn_type->t_tspec);
3190
3191 l = tn->tn_left->tn_val->v_ldbl;
3192 if (modtab[tn->tn_op].m_binary)
3193 r = tn->tn_right->tn_val->v_ldbl;
3194
3195 switch (tn->tn_op) {
3196 case UPLUS:
3197 v->v_ldbl = l;
3198 break;
3199 case UMINUS:
3200 v->v_ldbl = -l;
3201 break;
3202 case MULT:
3203 v->v_ldbl = l * r;
3204 break;
3205 case DIV:
3206 if (r == 0.0) {
3207 /* division by 0 */
3208 error(139);
3209 if (t == FLOAT) {
3210 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
3211 } else if (t == DOUBLE) {
3212 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
3213 } else {
3214 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
3215 }
3216 } else {
3217 v->v_ldbl = l / r;
3218 }
3219 break;
3220 case PLUS:
3221 v->v_ldbl = l + r;
3222 break;
3223 case MINUS:
3224 v->v_ldbl = l - r;
3225 break;
3226 case LT:
3227 v->v_quad = (l < r) ? 1 : 0;
3228 break;
3229 case LE:
3230 v->v_quad = (l <= r) ? 1 : 0;
3231 break;
3232 case GE:
3233 v->v_quad = (l >= r) ? 1 : 0;
3234 break;
3235 case GT:
3236 v->v_quad = (l > r) ? 1 : 0;
3237 break;
3238 case EQ:
3239 v->v_quad = (l == r) ? 1 : 0;
3240 break;
3241 case NE:
3242 v->v_quad = (l != r) ? 1 : 0;
3243 break;
3244 default:
3245 lint_assert(/*CONSTCOND*/false);
3246 }
3247
3248 lint_assert(fpe != 0 || isnan((double)v->v_ldbl) == false);
3249 if (fpe != 0 || finite((double)v->v_ldbl) == false ||
3250 (t == FLOAT &&
3251 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
3252 (t == DOUBLE &&
3253 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
3254 /* floating point overflow detected, op %s */
3255 warning(142, modtab[tn->tn_op].m_name);
3256 if (t == FLOAT) {
3257 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
3258 } else if (t == DOUBLE) {
3259 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
3260 } else {
3261 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
3262 }
3263 fpe = 0;
3264 }
3265
3266 return new_constant_node(tn->tn_type, v);
3267 }
3268
3269
3270 /*
3271 * Create a constant node for sizeof.
3272 */
3273 tnode_t *
3274 build_sizeof(type_t *tp)
3275 {
3276 tspec_t st;
3277 #if SIZEOF_IS_ULONG
3278 st = ULONG;
3279 #else
3280 st = UINT;
3281 #endif
3282 return new_integer_constant_node(st, tsize(tp) / CHAR_SIZE);
3283 }
3284
3285 /*
3286 * Create a constant node for offsetof.
3287 */
3288 tnode_t *
3289 build_offsetof(type_t *tp, sym_t *sym)
3290 {
3291 tspec_t st;
3292 #if SIZEOF_IS_ULONG
3293 st = ULONG;
3294 #else
3295 st = UINT;
3296 #endif
3297 tspec_t t = tp->t_tspec;
3298 if (t != STRUCT && t != UNION)
3299 /* unacceptable operand of '%s' */
3300 error(111, "offsetof");
3301
3302 // XXX: wrong size, no checking for sym fixme
3303 return new_integer_constant_node(st, tsize(tp) / CHAR_SIZE);
3304 }
3305
3306 int64_t
3307 tsize(type_t *tp)
3308 {
3309 int elem, elsz;
3310 bool flex;
3311
3312 elem = 1;
3313 flex = false;
3314 while (tp->t_tspec == ARRAY) {
3315 flex = true; /* allow c99 flex arrays [] [0] */
3316 elem *= tp->t_dim;
3317 tp = tp->t_subt;
3318 }
3319 if (elem == 0) {
3320 if (!flex) {
3321 /* cannot take size/alignment of incomplete type */
3322 error(143);
3323 elem = 1;
3324 }
3325 }
3326 switch (tp->t_tspec) {
3327 case FUNC:
3328 /* cannot take size/alignment of function */
3329 error(144);
3330 elsz = 1;
3331 break;
3332 case STRUCT:
3333 case UNION:
3334 if (incompl(tp)) {
3335 /* cannot take size/alignment of incomplete type */
3336 error(143);
3337 elsz = 1;
3338 } else {
3339 elsz = tp->t_str->size;
3340 }
3341 break;
3342 case ENUM:
3343 if (incompl(tp)) {
3344 /* cannot take size/alignment of incomplete type */
3345 warning(143);
3346 }
3347 /* FALLTHROUGH */
3348 default:
3349 if (tp->t_bitfield) {
3350 /* cannot take size/alignment of bit-field */
3351 error(145);
3352 }
3353 if (tp->t_tspec == VOID) {
3354 /* cannot take size/alignment of void */
3355 error(146);
3356 elsz = 1;
3357 } else {
3358 elsz = size(tp->t_tspec);
3359 lint_assert(elsz > 0);
3360 }
3361 break;
3362 }
3363
3364 /* XXX: type conversion is too late */
3365 return (int64_t)(elem * elsz);
3366 }
3367
3368 /*
3369 */
3370 tnode_t *
3371 build_alignof(type_t *tp)
3372 {
3373 tspec_t st;
3374
3375 switch (tp->t_tspec) {
3376 case ARRAY:
3377 break;
3378
3379 case FUNC:
3380 /* cannot take size/alignment of function */
3381 error(144);
3382 return 0;
3383
3384 case STRUCT:
3385 case UNION:
3386 if (incompl(tp)) {
3387 /* cannot take size/alignment of incomplete type */
3388 error(143);
3389 return 0;
3390 }
3391 break;
3392 case ENUM:
3393 break;
3394 default:
3395 if (tp->t_bitfield) {
3396 /* cannot take size/alignment of bit-field */
3397 error(145);
3398 return 0;
3399 }
3400 if (tp->t_tspec == VOID) {
3401 /* cannot take size/alignment of void */
3402 error(146);
3403 return 0;
3404 }
3405 break;
3406 }
3407
3408 #if SIZEOF_IS_ULONG
3409 st = ULONG;
3410 #else
3411 st = UINT;
3412 #endif
3413
3414 return new_integer_constant_node(st, (int64_t)getbound(tp) / CHAR_SIZE);
3415 }
3416
3417 /*
3418 * Type casts.
3419 */
3420 tnode_t *
3421 cast(tnode_t *tn, type_t *tp)
3422 {
3423 tspec_t nt, ot;
3424
3425 if (tn == NULL)
3426 return NULL;
3427
3428 tn = cconv(tn);
3429
3430 nt = tp->t_tspec;
3431 ot = tn->tn_type->t_tspec;
3432
3433 if (nt == VOID) {
3434 /*
3435 * XXX ANSI C requires scalar types or void (Plauger & Brodie).
3436 * But this seams really questionable.
3437 */
3438 } else if (nt == UNION) {
3439 sym_t *m;
3440 str_t *str = tp->t_str;
3441 if (!Sflag) {
3442 /* union cast is a C9X feature */
3443 error(328);
3444 return NULL;
3445 }
3446 for (m = str->memb; m != NULL; m = m->s_next) {
3447 if (sametype(m->s_type, tn->tn_type)) {
3448 tn = getnode();
3449 tn->tn_op = CVT;
3450 tn->tn_type = tp;
3451 tn->tn_cast = true;
3452 tn->tn_right = NULL;
3453 return tn;
3454 }
3455 }
3456 /* type '%s' is not a member of '%s' */
3457 error(329, type_name(tn->tn_type), type_name(tp));
3458 return NULL;
3459 } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
3460 if (!Sflag || nt == ARRAY || nt == FUNC) {
3461 /* invalid cast expression */
3462 error(147);
3463 return NULL;
3464 }
3465 } else if (ot == STRUCT || ot == UNION) {
3466 /* invalid cast expression */
3467 error(147);
3468 return NULL;
3469 } else if (ot == VOID) {
3470 /* improper cast of void expression */
3471 error(148);
3472 return NULL;
3473 } else if (is_integer(nt) && is_scalar(ot)) {
3474 /* ok */
3475 } else if (is_floating(nt) && is_arithmetic(ot)) {
3476 /* ok */
3477 } else if (nt == PTR && is_integer(ot)) {
3478 /* ok */
3479 } else if (nt == PTR && ot == PTR) {
3480 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3481 if (hflag)
3482 /* cast discards 'const' from ... */
3483 warning(275);
3484 }
3485 } else {
3486 /* invalid cast expression */
3487 error(147);
3488 return NULL;
3489 }
3490
3491 tn = convert(CVT, 0, tp, tn);
3492 tn->tn_cast = true;
3493
3494 return tn;
3495 }
3496
3497 /*
3498 * Create the node for a function argument.
3499 * All necessary conversions and type checks are done in
3500 * new_function_call_node because new_function_argument_node has no
3501 * information about expected argument types.
3502 */
3503 tnode_t *
3504 new_function_argument_node(tnode_t *args, tnode_t *arg)
3505 {
3506 tnode_t *ntn;
3507
3508 /*
3509 * If there was a serious error in the expression for the argument,
3510 * create a dummy argument so the positions of the remaining arguments
3511 * will not change.
3512 */
3513 if (arg == NULL)
3514 arg = new_integer_constant_node(INT, (int64_t)0);
3515
3516 ntn = new_tnode(PUSH, arg->tn_type, arg, args);
3517
3518 return ntn;
3519 }
3520
3521 /*
3522 * Create the node for a function call. Also check types of
3523 * function arguments and insert conversions, if necessary.
3524 */
3525 tnode_t *
3526 new_function_call_node(tnode_t *func, tnode_t *args)
3527 {
3528 tnode_t *ntn;
3529 op_t fcop;
3530
3531 if (func == NULL)
3532 return NULL;
3533
3534 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3535 fcop = CALL;
3536 } else {
3537 fcop = ICALL;
3538 }
3539
3540 /*
3541 * after cconv() func will always be a pointer to a function
3542 * if it is a valid function designator.
3543 */
3544 func = cconv(func);
3545
3546 if (func->tn_type->t_tspec != PTR ||
3547 func->tn_type->t_subt->t_tspec != FUNC) {
3548 /* illegal function (type %s) */
3549 error(149, type_name(func->tn_type));
3550 return NULL;
3551 }
3552
3553 args = check_function_arguments(func->tn_type->t_subt, args);
3554
3555 ntn = new_tnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3556
3557 return ntn;
3558 }
3559
3560 /*
3561 * Check types of all function arguments and insert conversions,
3562 * if necessary.
3563 */
3564 static tnode_t *
3565 check_function_arguments(type_t *ftp, tnode_t *args)
3566 {
3567 tnode_t *arg;
3568 sym_t *asym;
3569 tspec_t at;
3570 int narg, npar, n, i;
3571
3572 /* get # of args in the prototype */
3573 npar = 0;
3574 for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
3575 npar++;
3576
3577 /* get # of args in function call */
3578 narg = 0;
3579 for (arg = args; arg != NULL; arg = arg->tn_right)
3580 narg++;
3581
3582 asym = ftp->t_args;
3583 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3584 /* argument mismatch: %d arg%s passed, %d expected */
3585 error(150, narg, narg > 1 ? "s" : "", npar);
3586 asym = NULL;
3587 }
3588
3589 for (n = 1; n <= narg; n++) {
3590
3591 /*
3592 * The rightmost argument is at the top of the argument
3593 * subtree.
3594 */
3595 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3596 continue;
3597
3598 /* some things which are always not allowed */
3599 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3600 /* void expressions may not be arguments, arg #%d */
3601 error(151, n);
3602 return NULL;
3603 } else if ((at == STRUCT || at == UNION) &&
3604 incompl(arg->tn_left->tn_type)) {
3605 /* argument cannot have unknown size, arg #%d */
3606 error(152, n);
3607 return NULL;
3608 } else if (is_integer(at) &&
3609 arg->tn_left->tn_type->t_isenum &&
3610 incompl(arg->tn_left->tn_type)) {
3611 /* argument cannot have unknown size, arg #%d */
3612 warning(152, n);
3613 }
3614
3615 /* class conversions (arg in value context) */
3616 arg->tn_left = cconv(arg->tn_left);
3617
3618 if (asym != NULL) {
3619 arg->tn_left = check_prototype_argument(
3620 n, asym->s_type, arg->tn_left);
3621 } else {
3622 arg->tn_left = promote(NOOP, 1, arg->tn_left);
3623 }
3624 arg->tn_type = arg->tn_left->tn_type;
3625
3626 if (asym != NULL)
3627 asym = asym->s_next;
3628 }
3629
3630 return args;
3631 }
3632
3633 /*
3634 * Compare the type of an argument with the corresponding type of a
3635 * prototype parameter. If it is a valid combination, but both types
3636 * are not the same, insert a conversion to convert the argument into
3637 * the type of the parameter.
3638 */
3639 static tnode_t *
3640 check_prototype_argument(
3641 int n, /* pos of arg */
3642 type_t *tp, /* expected type (from prototype) */
3643 tnode_t *tn) /* argument */
3644 {
3645 tnode_t *ln;
3646 bool dowarn;
3647
3648 ln = xcalloc(1, sizeof (tnode_t));
3649 ln->tn_type = tduptyp(tp);
3650 ln->tn_type->t_const = false;
3651 ln->tn_lvalue = true;
3652 if (typeok(FARG, n, ln, tn)) {
3653 if (!eqtype(tp, tn->tn_type,
3654 true, false, (dowarn = false, &dowarn)) || dowarn)
3655 tn = convert(FARG, n, tp, tn);
3656 }
3657 free(ln);
3658 return tn;
3659 }
3660
3661 /*
3662 * Return the value of an integral constant expression.
3663 * If the expression is not constant or its type is not an integer
3664 * type, an error message is printed.
3665 */
3666 val_t *
3667 constant(tnode_t *tn, bool required)
3668 {
3669 val_t *v;
3670
3671 if (tn != NULL)
3672 tn = cconv(tn);
3673 if (tn != NULL)
3674 tn = promote(NOOP, 0, tn);
3675
3676 v = xcalloc(1, sizeof (val_t));
3677
3678 if (tn == NULL) {
3679 lint_assert(nerr != 0);
3680 if (dflag)
3681 printf("constant node is null; returning 1 instead\n");
3682 v->v_tspec = INT;
3683 v->v_quad = 1;
3684 return v;
3685 }
3686
3687 v->v_tspec = tn->tn_type->t_tspec;
3688
3689 if (tn->tn_op == CON) {
3690 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
3691 if (is_integer(tn->tn_val->v_tspec)) {
3692 v->v_ansiu = tn->tn_val->v_ansiu;
3693 v->v_quad = tn->tn_val->v_quad;
3694 return v;
3695 }
3696 v->v_quad = tn->tn_val->v_ldbl;
3697 } else {
3698 v->v_quad = 1;
3699 }
3700
3701 if (required)
3702 /* integral constant expression expected */
3703 error(55);
3704 else
3705 /* variable array dimension is a C99/GCC extension */
3706 c99ism(318);
3707
3708 if (!is_integer(v->v_tspec))
3709 v->v_tspec = INT;
3710
3711 return v;
3712 }
3713
3714 /*
3715 * Perform some tests on expressions which can't be done in build() and
3716 * functions called by build(). These tests must be done here because
3717 * we need some information about the context in which the operations
3718 * are performed.
3719 * After all tests are performed, expr() frees the memory which is used
3720 * for the expression.
3721 */
3722 void
3723 expr(tnode_t *tn, bool vctx, bool tctx, bool dofreeblk)
3724 {
3725
3726 lint_assert(tn != NULL || nerr != 0);
3727
3728 if (tn == NULL) {
3729 tfreeblk();
3730 return;
3731 }
3732
3733 /* expr() is also called in global initialisations */
3734 if (dcs->d_ctx != EXTERN)
3735 check_statement_reachable();
3736
3737 check_expr_misc(tn, vctx, tctx, !tctx, 0, 0, 0);
3738 if (tn->tn_op == ASSIGN) {
3739 if (hflag && tctx)
3740 /* assignment in conditional context */
3741 warning(159);
3742 } else if (tn->tn_op == CON) {
3743 if (hflag && tctx && !constcond_flag)
3744 /* constant in conditional context */
3745 warning(161);
3746 }
3747 if (!modtab[tn->tn_op].m_sideeff) {
3748 /*
3749 * for left operands of COMMA this warning is already
3750 * printed
3751 */
3752 if (tn->tn_op != COMMA && !vctx && !tctx)
3753 check_null_effect(tn);
3754 }
3755 if (dflag)
3756 display_expression(tn, 0);
3757
3758 /* free the tree memory */
3759 if (dofreeblk)
3760 tfreeblk();
3761 }
3762
3763 static void
3764 check_null_effect(const tnode_t *tn)
3765 {
3766
3767 if (!hflag)
3768 return;
3769
3770 while (!modtab[tn->tn_op].m_sideeff) {
3771 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3772 tn = tn->tn_left;
3773 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3774 /*
3775 * && and || have a side effect if the right operand
3776 * has a side effect.
3777 */
3778 tn = tn->tn_right;
3779 } else if (tn->tn_op == QUEST) {
3780 /*
3781 * ? has a side effect if at least one of its right
3782 * operands has a side effect
3783 */
3784 tn = tn->tn_right;
3785 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) {
3786 /*
3787 * : has a side effect if at least one of its operands
3788 * has a side effect
3789 */
3790 if (modtab[tn->tn_left->tn_op].m_sideeff) {
3791 tn = tn->tn_left;
3792 } else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3793 tn = tn->tn_right;
3794 } else {
3795 break;
3796 }
3797 } else {
3798 break;
3799 }
3800 }
3801 if (!modtab[tn->tn_op].m_sideeff)
3802 /* expression has null effect */
3803 warning(129);
3804 }
3805
3806 /*
3807 * Dump an expression to stdout
3808 * only used for debugging
3809 */
3810 static void
3811 display_expression(const tnode_t *tn, int offs)
3812 {
3813 uint64_t uq;
3814
3815 if (tn == NULL) {
3816 (void)printf("%*s%s\n", offs, "", "NULL");
3817 return;
3818 }
3819 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
3820
3821 if (tn->tn_op == NAME) {
3822 (void)printf("%s: %s ",
3823 tn->tn_sym->s_name,
3824 storage_class_name(tn->tn_sym->s_scl));
3825 } else if (tn->tn_op == CON && is_floating(tn->tn_type->t_tspec)) {
3826 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3827 } else if (tn->tn_op == CON && is_integer(tn->tn_type->t_tspec)) {
3828 uq = tn->tn_val->v_quad;
3829 (void)printf("0x %08lx %08lx ",
3830 (long)(uq >> 32) & 0xffffffffl,
3831 (long)uq & 0xffffffffl);
3832 } else if (tn->tn_op == CON) {
3833 lint_assert(tn->tn_type->t_tspec == PTR);
3834 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3835 (u_long)tn->tn_val->v_quad);
3836 } else if (tn->tn_op == STRING) {
3837 if (tn->tn_string->st_tspec == CHAR) {
3838 (void)printf("\"%s\"", tn->tn_string->st_cp);
3839 } else {
3840 char *s;
3841 size_t n;
3842 n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
3843 s = xmalloc(n);
3844 (void)wcstombs(s, tn->tn_string->st_wcp, n);
3845 (void)printf("L\"%s\"", s);
3846 free(s);
3847 }
3848 (void)printf(" ");
3849 } else if (tn->tn_op == FSEL) {
3850 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3851 tn->tn_type->t_flen);
3852 }
3853 (void)printf("%s\n", ttos(tn->tn_type));
3854 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3855 return;
3856 display_expression(tn->tn_left, offs + 2);
3857 if (modtab[tn->tn_op].m_binary ||
3858 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3859 display_expression(tn->tn_right, offs + 2);
3860 }
3861 }
3862
3863 /*
3864 * Called by expr() to recursively perform some tests.
3865 */
3866 /* ARGSUSED */
3867 void
3868 check_expr_misc(const tnode_t *tn, bool vctx, bool tctx,
3869 bool eqwarn, bool fcall, bool rvdisc, bool szof)
3870 {
3871 tnode_t *ln, *rn;
3872 mod_t *mp;
3873 bool nrvdisc, cvctx, ctctx;
3874 op_t op;
3875 scl_t sc;
3876 dinfo_t *di;
3877
3878 if (tn == NULL)
3879 return;
3880
3881 ln = tn->tn_left;
3882 rn = tn->tn_right;
3883 mp = &modtab[op = tn->tn_op];
3884
3885 switch (op) {
3886 case AMPER:
3887 if (ln->tn_op == NAME && (reached || rchflg)) {
3888 if (!szof)
3889 mark_as_set(ln->tn_sym);
3890 mark_as_used(ln->tn_sym, fcall, szof);
3891 }
3892 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3893 /* check the range of array indices */
3894 check_array_index(ln->tn_left, 1);
3895 break;
3896 case LOAD:
3897 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3898 /* check the range of array indices */
3899 check_array_index(ln->tn_left, 0);
3900 /* FALLTHROUGH */
3901 case PUSH:
3902 case INCBEF:
3903 case DECBEF:
3904 case INCAFT:
3905 case DECAFT:
3906 case ADDASS:
3907 case SUBASS:
3908 case MULASS:
3909 case DIVASS:
3910 case MODASS:
3911 case ANDASS:
3912 case ORASS:
3913 case XORASS:
3914 case SHLASS:
3915 case SHRASS:
3916 case REAL:
3917 case IMAG:
3918 if (ln->tn_op == NAME && (reached || rchflg)) {
3919 sc = ln->tn_sym->s_scl;
3920 /*
3921 * Look if there was a asm statement in one of the
3922 * compound statements we are in. If not, we don't
3923 * print a warning.
3924 */
3925 for (di = dcs; di != NULL; di = di->d_next) {
3926 if (di->d_asm)
3927 break;
3928 }
3929 if (sc != EXTERN && sc != STATIC &&
3930 !ln->tn_sym->s_set && !szof && di == NULL) {
3931 /* %s may be used before set */
3932 warning(158, ln->tn_sym->s_name);
3933 mark_as_set(ln->tn_sym);
3934 }
3935 mark_as_used(ln->tn_sym, 0, 0);
3936 }
3937 break;
3938 case ASSIGN:
3939 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3940 mark_as_set(ln->tn_sym);
3941 if (ln->tn_sym->s_scl == EXTERN)
3942 outusg(ln->tn_sym);
3943 }
3944 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3945 /* check the range of array indices */
3946 check_array_index(ln->tn_left, 0);
3947 break;
3948 case CALL:
3949 lint_assert(ln->tn_op == AMPER);
3950 lint_assert(ln->tn_left->tn_op == NAME);
3951 if (!szof)
3952 outcall(tn, vctx || tctx, rvdisc);
3953 break;
3954 case EQ:
3955 if (hflag && eqwarn)
3956 /* operator '==' found where '=' was expected */
3957 warning(160);
3958 break;
3959 case CON:
3960 case NAME:
3961 case STRING:
3962 return;
3963 /* LINTED206: (enumeration values not handled in switch) */
3964 case OR:
3965 case XOR:
3966 case NE:
3967 case GE:
3968 case GT:
3969 case LE:
3970 case LT:
3971 case SHR:
3972 case SHL:
3973 case MINUS:
3974 case PLUS:
3975 case MOD:
3976 case DIV:
3977 case MULT:
3978 case STAR:
3979 case UMINUS:
3980 case UPLUS:
3981 case DEC:
3982 case INC:
3983 case COMPL:
3984 case NOT:
3985 case POINT:
3986 case ARROW:
3987 case NOOP:
3988 case AND:
3989 case FARG:
3990 case CASE:
3991 case INIT:
3992 case RETURN:
3993 case ICALL:
3994 case CVT:
3995 case COMMA:
3996 case FSEL:
3997 case COLON:
3998 case QUEST:
3999 case LOGOR:
4000 case LOGAND:
4001 break;
4002 }
4003
4004 cvctx = mp->m_vctx;
4005 ctctx = mp->m_tctx;
4006 /*
4007 * values of operands of ':' are not used if the type of at least
4008 * one of the operands (for gcc compatibility) is void
4009 * XXX test/value context of QUEST should probably be used as
4010 * context for both operands of COLON
4011 */
4012 if (op == COLON && tn->tn_type->t_tspec == VOID)
4013 cvctx = ctctx = false;
4014 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
4015 check_expr_misc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
4016
4017 switch (op) {
4018 case PUSH:
4019 if (rn != NULL)
4020 check_expr_misc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
4021 break;
4022 case LOGAND:
4023 case LOGOR:
4024 check_expr_misc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
4025 break;
4026 case COLON:
4027 check_expr_misc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
4028 break;
4029 case COMMA:
4030 check_expr_misc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof);
4031 break;
4032 default:
4033 if (mp->m_binary)
4034 check_expr_misc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
4035 break;
4036 }
4037
4038 }
4039
4040 /*
4041 * Checks the range of array indices, if possible.
4042 * amper is set if only the address of the element is used. This
4043 * means that the index is allowed to refer to the first element
4044 * after the array.
4045 */
4046 static void
4047 check_array_index(tnode_t *tn, bool amper)
4048 {
4049 int dim;
4050 tnode_t *ln, *rn;
4051 int elsz;
4052 int64_t con;
4053
4054 ln = tn->tn_left;
4055 rn = tn->tn_right;
4056
4057 /* We can only check constant indices. */
4058 if (rn->tn_op != CON)
4059 return;
4060
4061 /* Return if the left node does not stem from an array. */
4062 if (ln->tn_op != AMPER)
4063 return;
4064 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
4065 return;
4066 if (ln->tn_left->tn_type->t_tspec != ARRAY)
4067 return;
4068
4069 /*
4070 * For incomplete array types, we can print a warning only if
4071 * the index is negative.
4072 */
4073 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
4074 return;
4075
4076 /* Get the size of one array element */
4077 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
4078 return;
4079 elsz /= CHAR_SIZE;
4080
4081 /* Change the unit of the index from bytes to element size. */
4082 if (is_uinteger(rn->tn_type->t_tspec)) {
4083 con = (uint64_t)rn->tn_val->v_quad / elsz;
4084 } else {
4085 con = rn->tn_val->v_quad / elsz;
4086 }
4087
4088 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
4089
4090 if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
4091 /* array subscript cannot be negative: %ld */
4092 warning(167, (long)con);
4093 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
4094 /* array subscript cannot be > %d: %ld */
4095 warning(168, dim - 1, (long)con);
4096 }
4097 }
4098
4099 /*
4100 * Check for ordered comparisons of unsigned values with 0.
4101 */
4102 static void
4103 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
4104 {
4105 tspec_t lt, rt;
4106 mod_t *mp;
4107
4108 lt = ln->tn_type->t_tspec;
4109 rt = rn->tn_type->t_tspec;
4110 mp = &modtab[op];
4111
4112 if (ln->tn_op != CON && rn->tn_op != CON)
4113 return;
4114
4115 if (!is_integer(lt) || !is_integer(rt))
4116 return;
4117
4118 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
4119 (rn->tn_val->v_quad < 0 ||
4120 rn->tn_val->v_quad > (int)~(~0U << (CHAR_SIZE - 1)))) {
4121 /* nonportable character comparison, op %s */
4122 warning(230, mp->m_name);
4123 return;
4124 }
4125 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
4126 (ln->tn_val->v_quad < 0 ||
4127 ln->tn_val->v_quad > (int)~(~0U << (CHAR_SIZE - 1)))) {
4128 /* nonportable character comparison, op %s */
4129 warning(230, mp->m_name);
4130 return;
4131 }
4132 if (is_uinteger(lt) && !is_uinteger(rt) &&
4133 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
4134 if (rn->tn_val->v_quad < 0) {
4135 /* comparison of %s with %s, op %s */
4136 warning(162, type_name(ln->tn_type),
4137 "negative constant", mp->m_name);
4138 } else if (op == LT || op == GE || (hflag && op == LE)) {
4139 /* comparison of %s with %s, op %s */
4140 warning(162, type_name(ln->tn_type), "0", mp->m_name);
4141 }
4142 return;
4143 }
4144 if (is_uinteger(rt) && !is_uinteger(lt) &&
4145 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
4146 if (ln->tn_val->v_quad < 0) {
4147 /* comparison of %s with %s, op %s */
4148 warning(162, "negative constant",
4149 type_name(rn->tn_type), mp->m_name);
4150 } else if (op == GT || op == LE || (hflag && op == GE)) {
4151 /* comparison of %s with %s, op %s */
4152 warning(162, "0", type_name(rn->tn_type), mp->m_name);
4153 }
4154 return;
4155 }
4156 }
4157
4158 /*
4159 * Takes an expression an returns 0 if this expression can be used
4160 * for static initialisation, otherwise -1.
4161 *
4162 * Constant initialisation expressions must be constant or an address
4163 * of a static object with an optional offset. In the first case,
4164 * the result is returned in *offsp. In the second case, the static
4165 * object is returned in *symp and the offset in *offsp.
4166 *
4167 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
4168 * CON. Type conversions are allowed if they do not change binary
4169 * representation (including width).
4170 */
4171 int
4172 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
4173 {
4174 sym_t *sym;
4175 ptrdiff_t offs1, offs2;
4176 tspec_t t, ot;
4177
4178 switch (tn->tn_op) {
4179 case MINUS:
4180 if (tn->tn_right->tn_op == CVT)
4181 return conaddr(tn->tn_right, symp, offsp);
4182 else if (tn->tn_right->tn_op != CON)
4183 return -1;
4184 /* FALLTHROUGH */
4185 case PLUS:
4186 offs1 = offs2 = 0;
4187 if (tn->tn_left->tn_op == CON) {
4188 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
4189 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
4190 return -1;
4191 } else if (tn->tn_right->tn_op == CON) {
4192 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
4193 if (tn->tn_op == MINUS)
4194 offs2 = -offs2;
4195 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
4196 return -1;
4197 } else {
4198 return -1;
4199 }
4200 *symp = sym;
4201 *offsp = offs1 + offs2;
4202 break;
4203 case AMPER:
4204 if (tn->tn_left->tn_op == NAME) {
4205 *symp = tn->tn_left->tn_sym;
4206 *offsp = 0;
4207 } else if (tn->tn_left->tn_op == STRING) {
4208 /*
4209 * If this would be the front end of a compiler we
4210 * would return a label instead of 0.
4211 */
4212 *offsp = 0;
4213 }
4214 break;
4215 case CVT:
4216 t = tn->tn_type->t_tspec;
4217 ot = tn->tn_left->tn_type->t_tspec;
4218 if ((!is_integer(t) && t != PTR) ||
4219 (!is_integer(ot) && ot != PTR)) {
4220 return -1;
4221 }
4222 #ifdef notdef
4223 /*
4224 * consider:
4225 * struct foo {
4226 * unsigned char a;
4227 * } f = {
4228 * (u_char)(u_long)(&(((struct foo *)0)->a))
4229 * };
4230 * since psize(u_long) != psize(u_char) this fails.
4231 */
4232 else if (psize(t) != psize(ot))
4233 return -1;
4234 #endif
4235 if (conaddr(tn->tn_left, symp, offsp) == -1)
4236 return -1;
4237 break;
4238 default:
4239 return -1;
4240 }
4241 return 0;
4242 }
4243
4244 /*
4245 * Concatenate two string constants.
4246 */
4247 strg_t *
4248 cat_strings(strg_t *strg1, strg_t *strg2)
4249 {
4250 size_t len1, len2, len;
4251
4252 if (strg1->st_tspec != strg2->st_tspec) {
4253 /* cannot concatenate wide and regular string literals */
4254 error(292);
4255 return strg1;
4256 }
4257
4258 len1 = strg1->st_len;
4259 len2 = strg2->st_len + 1; /* + NUL */
4260 len = len1 + len2;
4261
4262 #define COPY(F) \
4263 do { \
4264 strg1->F = xrealloc(strg1->F, len * sizeof(*strg1->F)); \
4265 (void)memcpy(strg1->F + len1, strg2->F, len2 * sizeof(*strg1->F)); \
4266 free(strg2->F); \
4267 } while (/*CONSTCOND*/false)
4268
4269 if (strg1->st_tspec == CHAR)
4270 COPY(st_cp);
4271 else
4272 COPY(st_wcp);
4273
4274 strg1->st_len = len - 1; /* - NUL */
4275 free(strg2);
4276
4277 return strg1;
4278 }
4279
4280 static bool
4281 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
4282 {
4283
4284 if (op == SHL || op == SHR) {
4285 if (!lparen && (lop == PLUS || lop == MINUS))
4286 return true;
4287 if (!rparen && (rop == PLUS || rop == MINUS))
4288 return true;
4289 return false;
4290 }
4291
4292 if (op == LOGOR) {
4293 if (!lparen && lop == LOGAND)
4294 return true;
4295 if (!rparen && rop == LOGAND)
4296 return true;
4297 return false;
4298 }
4299
4300 lint_assert(op == AND || op == XOR || op == OR);
4301 if (!lparen && lop != op) {
4302 if (lop == PLUS || lop == MINUS)
4303 return true;
4304 if (lop == AND || lop == XOR)
4305 return true;
4306 }
4307 if (!rparen && rop != op) {
4308 if (rop == PLUS || rop == MINUS)
4309 return true;
4310 if (rop == AND || rop == XOR)
4311 return true;
4312 }
4313 return false;
4314 }
4315
4316 /*
4317 * Print a warning if the given node has operands which should be
4318 * parenthesized.
4319 *
4320 * XXX Does not work if an operand is a constant expression. Constant
4321 * expressions are already folded.
4322 */
4323 static void
4324 check_precedence_confusion(tnode_t *tn)
4325 {
4326 tnode_t *ln, *rn;
4327
4328 if (!hflag)
4329 return;
4330
4331 dprint_node(tn);
4332
4333 lint_assert(modtab[tn->tn_op].m_binary);
4334 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4335 continue;
4336 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
4337 continue;
4338
4339 if (is_confusing_precedence(tn->tn_op,
4340 ln->tn_op, ln->tn_parenthesized,
4341 rn->tn_op, rn->tn_parenthesized)) {
4342 /* precedence confusion possible: parenthesize! */
4343 warning(169);
4344 }
4345 }
4346