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