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