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