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