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