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