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