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