tree.c revision 1.517 1 /* $NetBSD: tree.c,v 1.517 2023/04/22 17:49:15 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.517 2023/04/22 17:49:15 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_sou->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_sou == rn->tn_type->t_sou);
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_sou == rn->tn_type->t_sou);
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 if (t == DOUBLE)
1509 return lv < 0 ? -DBL_MAX : DBL_MAX;
1510 /*
1511 * When NetBSD is cross-built in MKLINT=yes mode on x86_64 for
1512 * sparc64, tools/lint checks this code while building usr.bin/xlint.
1513 * In that situation, lint uses the preprocessor for sparc64, in which
1514 * the type 'long double' is IEEE-754-binary128, affecting the macro
1515 * LDBL_MAX below. The type 'long double', as well as the strtold
1516 * implementation, comes from the host platform x86_64 though, where
1517 * 'long double' consumes 128 bits as well but only uses 80 of them.
1518 * The exponent range of the two 'long double' types is the same, but
1519 * the maximum finite value differs due to the extended precision on
1520 * sparc64.
1521 *
1522 * To properly handle the data types of the target platform, lint
1523 * would have to implement the floating-point types in a
1524 * platform-independent way, which is not worth the effort, given how
1525 * few programs practically use 'long double'.
1526 */
1527 /* LINTED 248: floating-point constant out of range */
1528 ldbl_t max = LDBL_MAX;
1529 return lv < 0 ? -max : max;
1530 }
1531
1532 /*
1533 * Fold constant nodes having operands with floating point type.
1534 */
1535 static tnode_t *
1536 fold_float(tnode_t *tn)
1537 {
1538 val_t *v;
1539 tspec_t t;
1540 ldbl_t lv, rv = 0;
1541
1542 fpe = 0;
1543 v = xcalloc(1, sizeof(*v));
1544 v->v_tspec = t = tn->tn_type->t_tspec;
1545
1546 lint_assert(is_floating(t));
1547 lint_assert(t == tn->tn_left->tn_type->t_tspec);
1548 lint_assert(!is_binary(tn) || t == tn->tn_right->tn_type->t_tspec);
1549
1550 lv = tn->tn_left->tn_val->v_ldbl;
1551 if (is_binary(tn))
1552 rv = tn->tn_right->tn_val->v_ldbl;
1553
1554 switch (tn->tn_op) {
1555 case UPLUS:
1556 v->v_ldbl = lv;
1557 break;
1558 case UMINUS:
1559 v->v_ldbl = -lv;
1560 break;
1561 case MULT:
1562 v->v_ldbl = lv * rv;
1563 break;
1564 case DIV:
1565 if (rv == 0.0) {
1566 /* division by 0 */
1567 error(139);
1568 v->v_ldbl = floating_error_value(t, lv);
1569 } else {
1570 v->v_ldbl = lv / rv;
1571 }
1572 break;
1573 case PLUS:
1574 v->v_ldbl = lv + rv;
1575 break;
1576 case MINUS:
1577 v->v_ldbl = lv - rv;
1578 break;
1579 case LT:
1580 v->v_quad = lv < rv ? 1 : 0;
1581 break;
1582 case LE:
1583 v->v_quad = lv <= rv ? 1 : 0;
1584 break;
1585 case GE:
1586 v->v_quad = lv >= rv ? 1 : 0;
1587 break;
1588 case GT:
1589 v->v_quad = lv > rv ? 1 : 0;
1590 break;
1591 case EQ:
1592 v->v_quad = lv == rv ? 1 : 0;
1593 break;
1594 case NE:
1595 v->v_quad = lv != rv ? 1 : 0;
1596 break;
1597 default:
1598 lint_assert(/*CONSTCOND*/false);
1599 }
1600
1601 lint_assert(fpe != 0 || isnan(v->v_ldbl) == 0);
1602 if (is_complex(v->v_tspec)) {
1603 /*
1604 * Don't warn, as lint doesn't model the imaginary part of
1605 * complex numbers.
1606 */
1607 fpe = 0;
1608 } else if (fpe != 0 || isfinite(v->v_ldbl) == 0 ||
1609 (t == FLOAT &&
1610 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
1611 (t == DOUBLE &&
1612 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
1613 /* floating point overflow on operator '%s' */
1614 warning(142, op_name(tn->tn_op));
1615 v->v_ldbl = floating_error_value(t, v->v_ldbl);
1616 fpe = 0;
1617 }
1618
1619 return build_constant(tn->tn_type, v);
1620 }
1621
1622 /*
1623 * Create a tree node for a binary operator and its two operands. Also called
1624 * for unary operators; in that case rn is NULL.
1625 *
1626 * Function calls, sizeof and casts are handled elsewhere.
1627 */
1628 tnode_t *
1629 build_binary(tnode_t *ln, op_t op, bool sys, tnode_t *rn)
1630 {
1631 const mod_t *mp;
1632 tnode_t *ntn;
1633 type_t *rettp;
1634
1635 mp = &modtab[op];
1636
1637 /* If there was an error in one of the operands, return. */
1638 if (ln == NULL || (mp->m_binary && rn == NULL))
1639 return NULL;
1640
1641 /*
1642 * Apply class conversions to the left operand, but only if its
1643 * value is needed or it is compared with zero.
1644 */
1645 if (mp->m_value_context || mp->m_compares_with_zero)
1646 ln = cconv(ln);
1647 /*
1648 * The right operand is almost always in a test or value context,
1649 * except if it is a struct or union member.
1650 */
1651 if (mp->m_binary && op != ARROW && op != POINT)
1652 rn = cconv(rn);
1653
1654 /*
1655 * Print some warnings for comparisons of unsigned values with
1656 * constants lower than or equal to null. This must be done
1657 * before promote() because otherwise unsigned char and unsigned
1658 * short would be promoted to int. Types are also tested to be
1659 * CHAR, which would also become int.
1660 */
1661 if (mp->m_comparison)
1662 check_integer_comparison(op, ln, rn);
1663
1664 if (mp->m_value_context || mp->m_compares_with_zero)
1665 ln = promote(op, false, ln);
1666 if (mp->m_binary && op != ARROW && op != POINT &&
1667 op != ASSIGN && op != RETURN && op != INIT) {
1668 rn = promote(op, false, rn);
1669 }
1670
1671 /*
1672 * If the result of the operation is different for signed or
1673 * unsigned operands and one of the operands is signed only in
1674 * ANSI C, print a warning.
1675 */
1676 if (mp->m_warn_if_left_unsigned_in_c90 &&
1677 ln->tn_op == CON && ln->tn_val->v_unsigned_since_c90) {
1678 /* ANSI C treats constant as unsigned, op '%s' */
1679 warning(218, mp->m_name);
1680 ln->tn_val->v_unsigned_since_c90 = false;
1681 }
1682 if (mp->m_warn_if_right_unsigned_in_c90 &&
1683 rn->tn_op == CON && rn->tn_val->v_unsigned_since_c90) {
1684 /* ANSI C treats constant as unsigned, op '%s' */
1685 warning(218, mp->m_name);
1686 rn->tn_val->v_unsigned_since_c90 = false;
1687 }
1688
1689 /* Make sure both operands are of the same type */
1690 if (mp->m_balance_operands || (!allow_c90 && (op == SHL || op == SHR)))
1691 balance(op, &ln, &rn);
1692
1693 /*
1694 * Check types for compatibility with the operation and mutual
1695 * compatibility. Return if there are serious problems.
1696 */
1697 if (!typeok(op, 0, ln, rn))
1698 return NULL;
1699
1700 /* And now create the node. */
1701 switch (op) {
1702 case POINT:
1703 case ARROW:
1704 ntn = build_struct_access(op, sys, ln, rn);
1705 break;
1706 case INCAFT:
1707 case DECAFT:
1708 case INCBEF:
1709 case DECBEF:
1710 ntn = build_prepost_incdec(op, sys, ln);
1711 break;
1712 case ADDR:
1713 ntn = build_address(sys, ln, false);
1714 break;
1715 case INDIR:
1716 ntn = new_tnode(INDIR, sys, ln->tn_type->t_subt, ln, NULL);
1717 break;
1718 case PLUS:
1719 case MINUS:
1720 ntn = build_plus_minus(op, sys, ln, rn);
1721 break;
1722 case SHL:
1723 case SHR:
1724 ntn = build_bit_shift(op, sys, ln, rn);
1725 break;
1726 case COLON:
1727 ntn = build_colon(sys, ln, rn);
1728 break;
1729 case ASSIGN:
1730 case MULASS:
1731 case DIVASS:
1732 case MODASS:
1733 case ADDASS:
1734 case SUBASS:
1735 case SHLASS:
1736 case SHRASS:
1737 case ANDASS:
1738 case XORASS:
1739 case ORASS:
1740 case RETURN:
1741 case INIT:
1742 ntn = build_assignment(op, sys, ln, rn);
1743 break;
1744 case COMMA:
1745 case QUEST:
1746 ntn = new_tnode(op, sys, rn->tn_type, ln, rn);
1747 break;
1748 case REAL:
1749 case IMAG:
1750 ntn = build_real_imag(op, sys, ln);
1751 break;
1752 default:
1753 rettp = mp->m_returns_bool
1754 ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
1755 lint_assert(mp->m_binary == (rn != NULL));
1756 ntn = new_tnode(op, sys, rettp, ln, rn);
1757 break;
1758 }
1759
1760 /* Return if an error occurred. */
1761 if (ntn == NULL)
1762 return NULL;
1763
1764 /* Print a warning if precedence confusion is possible */
1765 if (mp->m_possible_precedence_confusion)
1766 check_precedence_confusion(ntn);
1767
1768 /*
1769 * Print a warning if one of the operands is in a context where
1770 * it is compared with zero and if this operand is a constant.
1771 */
1772 if (hflag && !constcond_flag &&
1773 mp->m_compares_with_zero &&
1774 (ln->tn_op == CON ||
1775 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) &&
1776 /* XXX: rn->tn_system_dependent should be checked as well */
1777 !ln->tn_system_dependent) {
1778 /* constant in conditional context */
1779 warning(161);
1780 }
1781
1782 /* Fold if the operator requires it */
1783 if (mp->m_fold_constant_operands) {
1784 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
1785 if (mp->m_compares_with_zero) {
1786 ntn = fold_bool(ntn);
1787 } else if (is_floating(ntn->tn_type->t_tspec)) {
1788 ntn = fold_float(ntn);
1789 } else {
1790 ntn = fold(ntn);
1791 }
1792 } else if (op == QUEST && ln->tn_op == CON) {
1793 ntn = ln->tn_val->v_quad != 0
1794 ? rn->tn_left : rn->tn_right;
1795 }
1796 }
1797
1798 return ntn;
1799 }
1800
1801 tnode_t *
1802 build_unary(op_t op, bool sys, tnode_t *tn)
1803 {
1804 return build_binary(tn, op, sys, NULL);
1805 }
1806
1807 /*
1808 * Return whether all struct/union members with the same name have the same
1809 * type and offset.
1810 */
1811 static bool
1812 all_members_compatible(const sym_t *msym)
1813 {
1814 for (const sym_t *csym = msym;
1815 csym != NULL; csym = csym->s_symtab_next) {
1816 if (!is_member(csym))
1817 continue;
1818 if (strcmp(msym->s_name, csym->s_name) != 0)
1819 continue;
1820
1821 for (const sym_t *sym = csym->s_symtab_next;
1822 sym != NULL; sym = sym->s_symtab_next) {
1823
1824 if (!is_member(sym))
1825 continue;
1826 if (strcmp(csym->s_name, sym->s_name) != 0)
1827 continue;
1828 if (csym->u.s_member.sm_offset_in_bits !=
1829 sym->u.s_member.sm_offset_in_bits)
1830 return false;
1831
1832 bool w = false;
1833 if (!types_compatible(csym->s_type, sym->s_type,
1834 false, false, &w) && !w)
1835 return false;
1836 if (csym->s_bitfield != sym->s_bitfield)
1837 return false;
1838 if (csym->s_bitfield) {
1839 type_t *tp1 = csym->s_type;
1840 type_t *tp2 = sym->s_type;
1841 if (tp1->t_flen != tp2->t_flen)
1842 return false;
1843 if (tp1->t_foffs != tp2->t_foffs)
1844 return false;
1845 }
1846 }
1847 }
1848 return true;
1849 }
1850
1851 /*
1852 * Returns a symbol which has the same name as the msym argument and is a
1853 * member of the struct or union specified by the tn argument.
1854 */
1855 static sym_t *
1856 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
1857 {
1858 struct_or_union *str;
1859 type_t *tp;
1860 tspec_t t;
1861
1862 /*
1863 * Remove the member if it was unknown until now, which means
1864 * that no defined struct or union has a member with the same name.
1865 */
1866 if (msym->s_scl == NOSCL) {
1867 /* type '%s' does not have member '%s' */
1868 error(101, type_name(tn->tn_type), msym->s_name);
1869 rmsym(msym);
1870 msym->s_kind = FMEMBER;
1871 msym->s_scl = STRUCT_MEMBER;
1872
1873 struct_or_union *sou = expr_zero_alloc(sizeof(*sou));
1874 sou->sou_tag = expr_zero_alloc(sizeof(*sou->sou_tag));
1875 sou->sou_tag->s_name = unnamed;
1876
1877 msym->u.s_member.sm_sou_type = sou;
1878 /*
1879 * The member sm_offset_in_bits is not needed here since this
1880 * symbol can only be used for error reporting.
1881 */
1882 return msym;
1883 }
1884
1885 /* Set str to the tag of which msym is expected to be a member. */
1886 str = NULL;
1887 t = (tp = tn->tn_type)->t_tspec;
1888 if (op == POINT) {
1889 if (is_struct_or_union(t))
1890 str = tp->t_sou;
1891 } else if (op == ARROW && t == PTR) {
1892 t = (tp = tp->t_subt)->t_tspec;
1893 if (is_struct_or_union(t))
1894 str = tp->t_sou;
1895 }
1896
1897 /*
1898 * If this struct/union has a member with the name of msym, return it.
1899 */
1900 if (str != NULL) {
1901 for (sym_t *sym = msym;
1902 sym != NULL; sym = sym->s_symtab_next) {
1903 if (is_member(sym) &&
1904 sym->u.s_member.sm_sou_type == str &&
1905 strcmp(sym->s_name, msym->s_name) == 0)
1906 return sym;
1907 }
1908 }
1909
1910 bool eq = all_members_compatible(msym);
1911
1912 /*
1913 * Now handle the case in which the left operand refers really
1914 * to a struct/union, but the right operand is not member of it.
1915 */
1916 if (str != NULL) {
1917 if (eq && !allow_c90) {
1918 /* illegal use of member '%s' */
1919 warning(102, msym->s_name);
1920 } else {
1921 /* illegal use of member '%s' */
1922 error(102, msym->s_name);
1923 }
1924 return msym;
1925 }
1926
1927 /*
1928 * Now the left operand of ARROW does not point to a struct/union
1929 * or the left operand of POINT is no struct/union.
1930 */
1931 if (eq) {
1932 if (op == POINT) {
1933 if (!allow_c90) {
1934 /* left operand of '.' must be struct ... */
1935 warning(103, type_name(tn->tn_type));
1936 } else {
1937 /* left operand of '.' must be struct ... */
1938 error(103, type_name(tn->tn_type));
1939 }
1940 } else {
1941 if (!allow_c90 && tn->tn_type->t_tspec == PTR) {
1942 /* left operand of '->' must be pointer ... */
1943 warning(104, type_name(tn->tn_type));
1944 } else {
1945 /* left operand of '->' must be pointer ... */
1946 error(104, type_name(tn->tn_type));
1947 }
1948 }
1949 } else {
1950 if (!allow_c90) {
1951 /* non-unique member requires struct/union %s */
1952 error(105, op == POINT ? "object" : "pointer");
1953 } else {
1954 /* unacceptable operand of '%s' */
1955 error(111, op_name(op));
1956 }
1957 }
1958
1959 return msym;
1960 }
1961
1962 tnode_t *
1963 build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member)
1964 {
1965 sym_t *msym;
1966
1967 if (ln == NULL)
1968 return NULL;
1969
1970 if (op == ARROW) {
1971 /* must do this before struct_or_union_member is called */
1972 ln = cconv(ln);
1973 }
1974 msym = struct_or_union_member(ln, op, getsym(member));
1975 return build_binary(ln, op, sys, build_name(msym, false));
1976 }
1977
1978 /*
1979 * Perform class conversions.
1980 *
1981 * Arrays of type T are converted into pointers to type T.
1982 * Functions are converted to pointers to functions.
1983 * Lvalues are converted to rvalues.
1984 *
1985 * C99 6.3 "Conversions"
1986 * C99 6.3.2 "Other operands"
1987 * C99 6.3.2.1 "Lvalues, arrays, and function designators"
1988 */
1989 tnode_t *
1990 cconv(tnode_t *tn)
1991 {
1992 /*
1993 * Array-lvalue (array of type T) is converted into rvalue
1994 * (pointer to type T)
1995 */
1996 if (tn->tn_type->t_tspec == ARRAY) {
1997 if (!tn->tn_lvalue) {
1998 /* XXX print correct operator */
1999 /* %soperand of '%s' must be lvalue */
2000 gnuism(114, "", op_name(ADDR));
2001 }
2002 tn = new_tnode(ADDR, tn->tn_sys,
2003 expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL);
2004 }
2005
2006 /*
2007 * Expression of type function (function with return value of type T)
2008 * in rvalue-expression (pointer to function with return value
2009 * of type T)
2010 */
2011 if (tn->tn_type->t_tspec == FUNC)
2012 tn = build_address(tn->tn_sys, tn, true);
2013
2014 /* lvalue to rvalue */
2015 if (tn->tn_lvalue) {
2016 type_t *tp = expr_dup_type(tn->tn_type);
2017 /* C99 6.3.2.1p2 sentence 2 says to remove the qualifiers. */
2018 tp->t_const = tp->t_volatile = false;
2019 tn = new_tnode(LOAD, tn->tn_sys, tp, tn, NULL);
2020 }
2021
2022 return tn;
2023 }
2024
2025 const tnode_t *
2026 before_conversion(const tnode_t *tn)
2027 {
2028 while (tn->tn_op == CVT && !tn->tn_cast)
2029 tn = tn->tn_left;
2030 return tn;
2031 }
2032
2033 /*
2034 * Most errors required by ANSI C are reported in struct_or_union_member().
2035 * Here we only check for totally wrong things.
2036 */
2037 static bool
2038 typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt)
2039 {
2040 if (is_struct_or_union(lt))
2041 return true;
2042
2043 if (lt == FUNC || lt == VOID || ltp->t_bitfield)
2044 goto wrong;
2045
2046 /*
2047 * Some C dialects from before C90 tolerated any lvalue on the
2048 * left-hand side of the '.' operator, allowing things like
2049 * char st[100]; st.st_mtime, assuming that the member 'st_mtime'
2050 * only occurred in a single struct; see typeok_arrow.
2051 */
2052 if (ln->tn_lvalue)
2053 return true;
2054
2055 wrong:
2056 /* With allow_c90 we already got an error */
2057 if (!allow_c90)
2058 /* unacceptable operand of '%s' */
2059 error(111, op_name(POINT));
2060
2061 return false;
2062 }
2063
2064 static bool
2065 typeok_arrow(tspec_t lt)
2066 {
2067 /*
2068 * C1978 Appendix A 14.1 says: <quote>In fact, any lvalue is allowed
2069 * before '.', and that lvalue is then assumed to have the form of
2070 * the structure of which the name of the right is a member. [...]
2071 * Such constructions are non-portable.</quote>
2072 */
2073 if (lt == PTR || (!allow_c90 && is_integer(lt)))
2074 return true;
2075
2076 /* With allow_c90 we already got an error */
2077 if (!allow_c90)
2078 /* unacceptable operand of '%s' */
2079 error(111, op_name(ARROW));
2080 return false;
2081 }
2082
2083 static bool
2084 typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp)
2085 {
2086 /* operand has scalar type (checked in typeok) */
2087 if (!tn->tn_lvalue) {
2088 if (tn->tn_op == CVT && tn->tn_cast &&
2089 tn->tn_left->tn_op == LOAD) {
2090 /* a cast does not yield an lvalue */
2091 error(163);
2092 }
2093 /* %soperand of '%s' must be lvalue */
2094 error(114, "", op_name(op));
2095 return false;
2096 }
2097 if (tp->t_const && allow_c90) {
2098 /* %soperand of '%s' must be modifiable lvalue */
2099 warning(115, "", op_name(op));
2100 }
2101 return true;
2102 }
2103
2104 static bool
2105 typeok_address(const mod_t *mp,
2106 const tnode_t *tn, const type_t *tp, tspec_t t)
2107 {
2108 if (t == ARRAY || t == FUNC) {
2109 /* ok, a warning comes later (in build_address()) */
2110 } else if (!tn->tn_lvalue) {
2111 if (tn->tn_op == CVT && tn->tn_cast &&
2112 tn->tn_left->tn_op == LOAD) {
2113 /* a cast does not yield an lvalue */
2114 error(163);
2115 }
2116 /* %soperand of '%s' must be lvalue */
2117 error(114, "", mp->m_name);
2118 return false;
2119 } else if (is_scalar(t)) {
2120 if (tp->t_bitfield) {
2121 /* cannot take address of bit-field */
2122 error(112);
2123 return false;
2124 }
2125 } else if (t != STRUCT && t != UNION) {
2126 /* unacceptable operand of '%s' */
2127 error(111, mp->m_name);
2128 return false;
2129 }
2130 if (tn->tn_op == NAME && tn->tn_sym->s_register) {
2131 /* cannot take address of register '%s' */
2132 error(113, tn->tn_sym->s_name);
2133 return false;
2134 }
2135 return true;
2136 }
2137
2138 static bool
2139 typeok_indir(const type_t *tp, tspec_t t)
2140 {
2141
2142 if (t != PTR) {
2143 /* cannot dereference non-pointer type '%s' */
2144 error(96, type_name(tp));
2145 return false;
2146 }
2147 return true;
2148 }
2149
2150 static void
2151 warn_incompatible_types(op_t op,
2152 const type_t *ltp, tspec_t lt,
2153 const type_t *rtp, tspec_t rt)
2154 {
2155 const mod_t *mp = &modtab[op];
2156
2157 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2158 /* void type illegal in expression */
2159 error(109);
2160 } else if (op == ASSIGN) {
2161 /* cannot assign to '%s' from '%s' */
2162 error(171, type_name(ltp), type_name(rtp));
2163 } else if (mp->m_binary) {
2164 /* operands of '%s' have incompatible types '%s' and '%s' */
2165 error(107, mp->m_name, tspec_name(lt), tspec_name(rt));
2166 } else {
2167 lint_assert(rt == NOTSPEC);
2168 /* operand of '%s' has invalid type '%s' */
2169 error(108, mp->m_name, type_name(ltp));
2170 }
2171 }
2172
2173 static bool
2174 typeok_plus(op_t op,
2175 const type_t *ltp, tspec_t lt,
2176 const type_t *rtp, tspec_t rt)
2177 {
2178 /* operands have scalar types (checked in typeok) */
2179 if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
2180 warn_incompatible_types(op, ltp, lt, rtp, rt);
2181 return false;
2182 }
2183 return true;
2184 }
2185
2186 static bool
2187 typeok_minus(op_t op,
2188 const type_t *ltp, tspec_t lt,
2189 const type_t *rtp, tspec_t rt)
2190 {
2191 /* operands have scalar types (checked in typeok) */
2192 if ((lt == PTR && rt != PTR && !is_integer(rt)) ||
2193 (lt != PTR && rt == PTR)) {
2194 warn_incompatible_types(op, ltp, lt, rtp, rt);
2195 return false;
2196 }
2197 if (lt == PTR && rt == PTR &&
2198 !types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
2199 /* illegal pointer subtraction */
2200 error(116);
2201 }
2202 return true;
2203 }
2204
2205 static void
2206 typeok_shr(const mod_t *mp,
2207 const tnode_t *ln, tspec_t lt,
2208 const tnode_t *rn, tspec_t rt)
2209 {
2210 tspec_t olt, ort;
2211
2212 olt = before_conversion(ln)->tn_type->t_tspec;
2213 ort = before_conversion(rn)->tn_type->t_tspec;
2214
2215 /* operands have integer types (checked in typeok) */
2216 if (pflag && !is_uinteger(olt)) {
2217 integer_constraints lc = ic_expr(ln);
2218 if (!ic_maybe_signed(ln->tn_type, &lc))
2219 return;
2220
2221 /*
2222 * The left operand is signed. This means that
2223 * the operation is (possibly) nonportable.
2224 */
2225 if (ln->tn_op != CON) {
2226 /* bitwise '%s' on signed value possibly nonportable */
2227 warning(117, mp->m_name);
2228 } else if (ln->tn_val->v_quad < 0) {
2229 /* bitwise '%s' on signed value nonportable */
2230 warning(120, mp->m_name);
2231 }
2232 } else if (allow_trad && allow_c90 &&
2233 !is_uinteger(olt) && is_uinteger(ort)) {
2234 /* The left operand would become unsigned in traditional C. */
2235 if (hflag && (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
2236 /* semantics of '%s' change in ANSI C; use ... */
2237 warning(118, mp->m_name);
2238 }
2239 } else if (allow_trad && allow_c90 &&
2240 !is_uinteger(olt) && !is_uinteger(ort) &&
2241 portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
2242 /*
2243 * In traditional C the left operand would be extended
2244 * (possibly sign-extended) and then shifted.
2245 */
2246 if (hflag && (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
2247 /* semantics of '%s' change in ANSI C; use ... */
2248 warning(118, mp->m_name);
2249 }
2250 }
2251 }
2252
2253 static void
2254 typeok_shl(const mod_t *mp, tspec_t lt, tspec_t rt)
2255 {
2256 /*
2257 * C90 does not perform balancing for shift operations,
2258 * but traditional C does. If the width of the right operand
2259 * is greater than the width of the left operand, then in
2260 * traditional C the left operand would be extended to the
2261 * width of the right operand. For SHL this may result in
2262 * different results.
2263 */
2264 if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
2265 /*
2266 * XXX If both operands are constant, make sure
2267 * that there is really a difference between
2268 * ANSI C and traditional C.
2269 */
2270 if (hflag && !allow_c99)
2271 /* semantics of '%s' change in ANSI C; use ... */
2272 warning(118, mp->m_name);
2273 }
2274 }
2275
2276 static void
2277 typeok_shift(const type_t *ltp, tspec_t lt, const tnode_t *rn, tspec_t rt)
2278 {
2279 if (rn->tn_op != CON)
2280 return;
2281
2282 if (!is_uinteger(rt) && rn->tn_val->v_quad < 0) {
2283 /* negative shift */
2284 warning(121);
2285 } else if ((uint64_t)rn->tn_val->v_quad ==
2286 (uint64_t)size_in_bits(lt)) {
2287 /* shift amount %u equals bit-size of '%s' */
2288 warning(267, (unsigned)rn->tn_val->v_quad, type_name(ltp));
2289 } else if ((uint64_t)rn->tn_val->v_quad > (uint64_t)size_in_bits(lt)) {
2290 /* shift amount %llu is greater than bit-size %llu of '%s' */
2291 warning(122, (unsigned long long)rn->tn_val->v_quad,
2292 (unsigned long long)size_in_bits(lt),
2293 tspec_name(lt));
2294 }
2295 }
2296
2297 static bool
2298 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
2299 {
2300 if (lt == PTR && is_null_pointer(rn))
2301 return true;
2302 if (rt == PTR && is_null_pointer(ln))
2303 return true;
2304 return false;
2305 }
2306
2307 /*
2308 * Called if incompatible pointer types are detected.
2309 * Print an appropriate warning.
2310 */
2311 static void
2312 warn_incompatible_pointers(const mod_t *mp,
2313 const type_t *ltp, const type_t *rtp)
2314 {
2315 lint_assert(ltp->t_tspec == PTR);
2316 lint_assert(rtp->t_tspec == PTR);
2317
2318 tspec_t lt = ltp->t_subt->t_tspec;
2319 tspec_t rt = rtp->t_subt->t_tspec;
2320
2321 if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
2322 if (mp == NULL) {
2323 /* illegal structure pointer combination */
2324 warning(244);
2325 } else {
2326 /* incompatible structure pointers: '%s' '%s' '%s' */
2327 warning(245, type_name(ltp), mp->m_name, type_name(rtp));
2328 }
2329 } else {
2330 if (mp == NULL) {
2331 /* illegal combination of '%s' and '%s' */
2332 warning(184, type_name(ltp), type_name(rtp));
2333 } else {
2334 /* illegal combination of '%s' and '%s', op '%s' */
2335 warning(124,
2336 type_name(ltp), type_name(rtp), mp->m_name);
2337 }
2338 }
2339 }
2340
2341 static void
2342 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
2343 {
2344 type_t *ltp = ln->tn_type, *rtp = rn->tn_type;
2345 tspec_t lst = ltp->t_subt->t_tspec, rst = rtp->t_subt->t_tspec;
2346
2347 if (lst == VOID || rst == VOID) {
2348 /* TODO: C99 behaves like C90 here. */
2349 if ((!allow_trad && !allow_c99) &&
2350 (lst == FUNC || rst == FUNC)) {
2351 /* (void *)0 is already handled in typeok() */
2352 const char *lsts, *rsts;
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 if (lt == PTR && rt == PTR) {
2380 check_pointer_comparison(op, ln, rn);
2381 return true;
2382 }
2383
2384 if (lt != PTR && rt != PTR)
2385 return true;
2386
2387 if (!is_integer(lt) && !is_integer(rt)) {
2388 warn_incompatible_types(op, ltp, lt, rtp, rt);
2389 return false;
2390 }
2391
2392 const char *lx = lt == PTR ? "pointer" : "integer";
2393 const char *rx = rt == PTR ? "pointer" : "integer";
2394 /* illegal combination of %s '%s' and %s '%s', op '%s' */
2395 warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
2396 return true;
2397 }
2398
2399 static bool
2400 typeok_quest(tspec_t lt, const tnode_t *rn)
2401 {
2402 if (!is_scalar(lt)) {
2403 /* first operand must have scalar type, op ? : */
2404 error(170);
2405 return false;
2406 }
2407 lint_assert(before_conversion(rn)->tn_op == COLON);
2408 return true;
2409 }
2410
2411 static void
2412 typeok_colon_pointer(const mod_t *mp, const type_t *ltp, const type_t *rtp)
2413 {
2414 type_t *lstp = ltp->t_subt;
2415 type_t *rstp = rtp->t_subt;
2416 tspec_t lst = lstp->t_tspec;
2417 tspec_t rst = rstp->t_tspec;
2418
2419 if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
2420 /* (void *)0 is handled in typeok_colon */
2421 /* TODO: C99 behaves like C90 here. */
2422 if (!allow_trad && !allow_c99)
2423 /* ANSI C forbids conversion of %s to %s, op %s */
2424 warning(305, "function pointer", "'void *'",
2425 mp->m_name);
2426 return;
2427 }
2428
2429 if (pointer_types_are_compatible(lstp, rstp, true))
2430 return;
2431 if (!types_compatible(lstp, rstp, true, false, NULL))
2432 warn_incompatible_pointers(mp, ltp, rtp);
2433 }
2434
2435 static bool
2436 typeok_colon(const mod_t *mp,
2437 const tnode_t *ln, const type_t *ltp, tspec_t lt,
2438 const tnode_t *rn, const type_t *rtp, tspec_t rt)
2439 {
2440
2441 if (is_arithmetic(lt) && is_arithmetic(rt))
2442 return true;
2443 if (lt == BOOL && rt == BOOL)
2444 return true;
2445
2446 if (lt == STRUCT && rt == STRUCT && ltp->t_sou == rtp->t_sou)
2447 return true;
2448 if (lt == UNION && rt == UNION && ltp->t_sou == rtp->t_sou)
2449 return true;
2450
2451 if (lt == PTR && is_null_pointer(rn))
2452 return true;
2453 if (rt == PTR && is_null_pointer(ln))
2454 return true;
2455
2456 if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
2457 const char *lx = lt == PTR ? "pointer" : "integer";
2458 const char *rx = rt == PTR ? "pointer" : "integer";
2459 /* illegal combination of %s '%s' and %s '%s', op '%s' */
2460 warning(123, lx, type_name(ltp),
2461 rx, type_name(rtp), mp->m_name);
2462 return true;
2463 }
2464
2465 if (lt == VOID || rt == VOID) {
2466 if (lt != VOID || rt != VOID)
2467 /* incompatible types '%s' and '%s' in conditional */
2468 warning(126, type_name(ltp), type_name(rtp));
2469 return true;
2470 }
2471
2472 if (lt == PTR && rt == PTR) {
2473 typeok_colon_pointer(mp, ltp, rtp);
2474 return true;
2475 }
2476
2477 /* incompatible types '%s' and '%s' in conditional */
2478 error(126, type_name(ltp), type_name(rtp));
2479 return false;
2480 }
2481
2482 /*
2483 * Returns true if the given structure or union has a constant member
2484 * (maybe recursively).
2485 */
2486 static bool
2487 has_constant_member(const type_t *tp)
2488 {
2489 lint_assert(is_struct_or_union(tp->t_tspec));
2490
2491 for (sym_t *m = tp->t_sou->sou_first_member;
2492 m != NULL; m = m->s_next) {
2493 const type_t *mtp = m->s_type;
2494 if (mtp->t_const)
2495 return true;
2496 if (is_struct_or_union(mtp->t_tspec) &&
2497 has_constant_member(mtp))
2498 return true;
2499 }
2500 return false;
2501 }
2502
2503 static bool
2504 typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
2505 {
2506 if (op == RETURN || op == INIT || op == FARG)
2507 return true;
2508
2509 if (!ln->tn_lvalue) {
2510 if (ln->tn_op == CVT && ln->tn_cast &&
2511 ln->tn_left->tn_op == LOAD) {
2512 /* a cast does not yield an lvalue */
2513 error(163);
2514 }
2515 /* %soperand of '%s' must be lvalue */
2516 error(114, "left ", op_name(op));
2517 return false;
2518 } else if (ltp->t_const || (is_struct_or_union(lt) &&
2519 has_constant_member(ltp))) {
2520 if (allow_c90)
2521 /* %soperand of '%s' must be modifiable lvalue */
2522 warning(115, "left ", op_name(op));
2523 }
2524 return true;
2525 }
2526
2527 /* Check the types using the information from modtab[]. */
2528 static bool
2529 typeok_scalar(op_t op, const mod_t *mp,
2530 const type_t *ltp, tspec_t lt,
2531 const type_t *rtp, tspec_t rt)
2532 {
2533 if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
2534 return true;
2535 if (mp->m_requires_integer) {
2536 if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
2537 warn_incompatible_types(op, ltp, lt, rtp, rt);
2538 return false;
2539 }
2540 } else if (mp->m_requires_integer_or_complex) {
2541 if ((!is_integer(lt) && !is_complex(lt)) ||
2542 (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
2543 warn_incompatible_types(op, ltp, lt, rtp, rt);
2544 return false;
2545 }
2546 } else if (mp->m_requires_scalar) {
2547 if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
2548 warn_incompatible_types(op, ltp, lt, rtp, rt);
2549 return false;
2550 }
2551 } else if (mp->m_requires_arith) {
2552 if (!is_arithmetic(lt) ||
2553 (mp->m_binary && !is_arithmetic(rt))) {
2554 warn_incompatible_types(op, ltp, lt, rtp, rt);
2555 return false;
2556 }
2557 }
2558 return true;
2559 }
2560
2561 static void
2562 check_assign_void_pointer(op_t op, int arg,
2563 tspec_t lt, tspec_t lst,
2564 tspec_t rt, tspec_t rst)
2565 {
2566
2567 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID)))
2568 return;
2569 /* two pointers, at least one pointer to void */
2570
2571 /* TODO: C99 behaves like C90 here. */
2572 if (!((!allow_trad && !allow_c99) && (lst == FUNC || rst == FUNC)))
2573 return;
2574 /* comb. of ptr to func and ptr to void */
2575
2576 const char *lts, *rts;
2577 *(lst == FUNC ? <s : &rts) = "function pointer";
2578 *(lst == VOID ? <s : &rts) = "'void *'";
2579
2580 switch (op) {
2581 case INIT:
2582 case RETURN:
2583 /* ANSI C forbids conversion of %s to %s */
2584 warning(303, rts, lts);
2585 break;
2586 case FARG:
2587 /* ANSI C forbids conversion of %s to %s, arg #%d */
2588 warning(304, rts, lts, arg);
2589 break;
2590 default:
2591 /* ANSI C forbids conversion of %s to %s, op %s */
2592 warning(305, rts, lts, op_name(op));
2593 break;
2594 }
2595 }
2596
2597 static bool
2598 is_direct_function_call(const tnode_t *tn, const char **out_name)
2599 {
2600
2601 if (!(tn->tn_op == CALL &&
2602 tn->tn_left->tn_op == ADDR &&
2603 tn->tn_left->tn_left->tn_op == NAME))
2604 return false;
2605
2606 *out_name = tn->tn_left->tn_left->tn_sym->s_name;
2607 return true;
2608 }
2609
2610 static bool
2611 is_unconst_function(const char *name)
2612 {
2613
2614 return strcmp(name, "memchr") == 0 ||
2615 strcmp(name, "strchr") == 0 ||
2616 strcmp(name, "strpbrk") == 0 ||
2617 strcmp(name, "strrchr") == 0 ||
2618 strcmp(name, "strstr") == 0;
2619 }
2620
2621 static bool
2622 is_const_char_pointer(const tnode_t *tn)
2623 {
2624 /*
2625 * For traditional reasons, C99 6.4.5p5 defines that string literals
2626 * have type 'char[]'. They are often implicitly converted to
2627 * 'char *', for example when they are passed as function arguments.
2628 *
2629 * C99 6.4.5p6 further defines that modifying a string that is
2630 * constructed from a string literal invokes undefined behavior.
2631 *
2632 * Out of these reasons, string literals are treated as 'effectively
2633 * const' here.
2634 */
2635 if (tn->tn_op == CVT &&
2636 tn->tn_left->tn_op == ADDR &&
2637 tn->tn_left->tn_left->tn_op == STRING)
2638 return true;
2639
2640 const type_t *tp = before_conversion(tn)->tn_type;
2641 return tp->t_tspec == PTR &&
2642 tp->t_subt->t_tspec == CHAR &&
2643 tp->t_subt->t_const;
2644 }
2645
2646 static bool
2647 is_first_arg_const_char_pointer(const tnode_t *tn)
2648 {
2649 const tnode_t *an = tn->tn_right;
2650 if (an == NULL)
2651 return false;
2652
2653 while (an->tn_right != NULL)
2654 an = an->tn_right;
2655 return is_const_char_pointer(an->tn_left);
2656 }
2657
2658 static bool
2659 is_const_pointer(const tnode_t *tn)
2660 {
2661 const type_t *tp = before_conversion(tn)->tn_type;
2662 return tp->t_tspec == PTR && tp->t_subt->t_const;
2663 }
2664
2665 static bool
2666 is_second_arg_const_pointer(const tnode_t *tn)
2667 {
2668 const tnode_t *an = tn->tn_right;
2669 if (an == NULL || an->tn_right == NULL)
2670 return false;
2671
2672 while (an->tn_right->tn_right != NULL)
2673 an = an->tn_right;
2674 return is_const_pointer(an->tn_left);
2675 }
2676
2677 static void
2678 check_unconst_function(const type_t *lstp, const tnode_t *rn)
2679 {
2680 const char *function_name;
2681
2682 if (lstp->t_tspec == CHAR && !lstp->t_const &&
2683 is_direct_function_call(rn, &function_name) &&
2684 is_unconst_function(function_name) &&
2685 is_first_arg_const_char_pointer(rn)) {
2686 /* call to '%s' effectively discards 'const' from argument */
2687 warning(346, function_name);
2688 }
2689
2690 if (!lstp->t_const &&
2691 is_direct_function_call(rn, &function_name) &&
2692 strcmp(function_name, "bsearch") == 0 &&
2693 is_second_arg_const_pointer(rn)) {
2694 /* call to '%s' effectively discards 'const' from argument */
2695 warning(346, function_name);
2696 }
2697 }
2698
2699 static bool
2700 check_assign_void_pointer_compat(op_t op, int arg,
2701 const type_t *const ltp, tspec_t const lt,
2702 const type_t *const lstp, tspec_t const lst,
2703 const tnode_t *const rn,
2704 const type_t *const rtp, tspec_t const rt,
2705 const type_t *const rstp, tspec_t const rst)
2706 {
2707 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
2708 types_compatible(lstp, rstp,
2709 true, false, NULL))))
2710 return false;
2711
2712 /* compatible pointer types (qualifiers ignored) */
2713 if (allow_c90 &&
2714 ((!lstp->t_const && rstp->t_const) ||
2715 (!lstp->t_volatile && rstp->t_volatile))) {
2716 /* left side has not all qualifiers of right */
2717 switch (op) {
2718 case INIT:
2719 case RETURN:
2720 /* incompatible pointer types to '%s' and '%s' */
2721 warning(182, type_name(lstp), type_name(rstp));
2722 break;
2723 case FARG:
2724 /* converting '%s' to incompatible '%s' ... */
2725 warning(153,
2726 type_name(rtp), type_name(ltp), arg);
2727 break;
2728 default:
2729 /* operands of '%s' have incompatible pointer ... */
2730 warning(128, op_name(op),
2731 type_name(lstp), type_name(rstp));
2732 break;
2733 }
2734 }
2735
2736 if (allow_c90)
2737 check_unconst_function(lstp, rn);
2738
2739 return true;
2740 }
2741
2742 static bool
2743 check_assign_pointer_integer(op_t op, int arg,
2744 const type_t *const ltp, tspec_t const lt,
2745 const type_t *const rtp, tspec_t const rt)
2746 {
2747 const char *lx, *rx;
2748
2749 if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)))
2750 return false;
2751
2752 lx = lt == PTR ? "pointer" : "integer";
2753 rx = rt == PTR ? "pointer" : "integer";
2754
2755 switch (op) {
2756 case INIT:
2757 case RETURN:
2758 /* illegal combination of %s '%s' and %s '%s' */
2759 warning(183, lx, type_name(ltp), rx, type_name(rtp));
2760 break;
2761 case FARG:
2762 /* illegal combination of %s '%s' and %s '%s', arg #%d */
2763 warning(154,
2764 lx, type_name(ltp), rx, type_name(rtp), arg);
2765 break;
2766 default:
2767 /* illegal combination of %s '%s' and %s '%s', op '%s' */
2768 warning(123,
2769 lx, type_name(ltp), rx, type_name(rtp), op_name(op));
2770 break;
2771 }
2772 return true;
2773 }
2774
2775 static bool
2776 check_assign_pointer(op_t op, int arg,
2777 const type_t *ltp, tspec_t lt,
2778 const type_t *rtp, tspec_t rt)
2779 {
2780 if (!(lt == PTR && rt == PTR))
2781 return false;
2782
2783 switch (op) {
2784 case RETURN:
2785 warn_incompatible_pointers(NULL, ltp, rtp);
2786 break;
2787 case FARG:
2788 /* converting '%s' to incompatible '%s' for ... */
2789 warning(153, type_name(rtp), type_name(ltp), arg);
2790 break;
2791 default:
2792 warn_incompatible_pointers(&modtab[op], ltp, rtp);
2793 break;
2794 }
2795 return true;
2796 }
2797
2798 static void
2799 warn_assign(op_t op, int arg,
2800 const type_t *ltp, tspec_t lt,
2801 const type_t *rtp, tspec_t rt)
2802 {
2803 switch (op) {
2804 case INIT:
2805 /* cannot initialize '%s' from '%s' */
2806 error(185, type_name(ltp), type_name(rtp));
2807 break;
2808 case RETURN:
2809 /* function has return type '%s' but returns '%s' */
2810 error(211, type_name(ltp), type_name(rtp));
2811 break;
2812 case FARG:
2813 /* passing '%s' to incompatible '%s', arg #%d */
2814 warning(155, type_name(rtp), type_name(ltp), arg);
2815 break;
2816 default:
2817 warn_incompatible_types(op, ltp, lt, rtp, rt);
2818 break;
2819 }
2820 }
2821
2822 /*
2823 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
2824 * and prints warnings/errors if necessary.
2825 * Returns whether the types are (almost) compatible.
2826 */
2827 static bool
2828 check_assign_types_compatible(op_t op, int arg,
2829 const tnode_t *ln, const tnode_t *rn)
2830 {
2831 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC;
2832 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
2833
2834 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
2835 lst = (lstp = ltp->t_subt)->t_tspec;
2836 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
2837 rst = (rstp = rtp->t_subt)->t_tspec;
2838
2839 if (lt == BOOL && is_scalar(rt)) /* C99 6.3.1.2 */
2840 return true;
2841
2842 if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
2843 return true;
2844
2845 if (is_struct_or_union(lt) && is_struct_or_union(rt))
2846 /* both are struct or union */
2847 return ltp->t_sou == rtp->t_sou;
2848
2849 /* a null pointer may be assigned to any pointer */
2850 if (lt == PTR && is_null_pointer(rn))
2851 return true;
2852
2853 check_assign_void_pointer(op, arg, lt, lst, rt, rst);
2854
2855 if (check_assign_void_pointer_compat(op, arg,
2856 ltp, lt, lstp, lst, rn, rtp, rt, rstp, rst))
2857 return true;
2858
2859 if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt))
2860 return true;
2861
2862 if (check_assign_pointer(op, arg, ltp, lt, rtp, rt))
2863 return true;
2864
2865 warn_assign(op, arg, ltp, lt, rtp, rt);
2866 return false;
2867 }
2868
2869 static bool
2870 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
2871 {
2872 op_t op = tn->tn_op;
2873
2874 if (modtab[op].m_has_side_effect)
2875 return true;
2876
2877 if (op == CVT && tn->tn_type->t_tspec == VOID)
2878 return has_side_effect(tn->tn_left);
2879
2880 /* XXX: Why not has_side_effect(tn->tn_left) as well? */
2881 if (op == LOGAND || op == LOGOR)
2882 return has_side_effect(tn->tn_right);
2883
2884 /* XXX: Why not has_side_effect(tn->tn_left) as well? */
2885 if (op == QUEST)
2886 return has_side_effect(tn->tn_right);
2887
2888 if (op == COLON || op == COMMA) {
2889 return has_side_effect(tn->tn_left) ||
2890 has_side_effect(tn->tn_right);
2891 }
2892
2893 return false;
2894 }
2895
2896 static bool
2897 is_void_cast(const tnode_t *tn)
2898 {
2899
2900 return tn->tn_op == CVT && tn->tn_cast &&
2901 tn->tn_type->t_tspec == VOID;
2902 }
2903
2904 static bool
2905 is_local_symbol(const tnode_t *tn)
2906 {
2907
2908 return tn->tn_op == LOAD &&
2909 tn->tn_left->tn_op == NAME &&
2910 tn->tn_left->tn_sym->s_scl == AUTO;
2911 }
2912
2913 static bool
2914 is_int_constant_zero(const tnode_t *tn)
2915 {
2916
2917 return tn->tn_op == CON &&
2918 tn->tn_type->t_tspec == INT &&
2919 tn->tn_val->v_quad == 0;
2920 }
2921
2922 static void
2923 check_null_effect(const tnode_t *tn)
2924 {
2925
2926 if (hflag &&
2927 !has_side_effect(tn) &&
2928 !(is_void_cast(tn) && is_local_symbol(tn->tn_left)) &&
2929 !(is_void_cast(tn) && is_int_constant_zero(tn->tn_left))) {
2930 /* expression has null effect */
2931 warning(129);
2932 }
2933 }
2934
2935 /*
2936 * Check the types for specific operators and type combinations.
2937 *
2938 * At this point, the operands already conform to the type requirements of
2939 * the operator, such as being integer, floating or scalar.
2940 */
2941 static bool
2942 typeok_op(op_t op, const mod_t *mp, int arg,
2943 const tnode_t *ln, const type_t *ltp, tspec_t lt,
2944 const tnode_t *rn, const type_t *rtp, tspec_t rt)
2945 {
2946 switch (op) {
2947 case ARROW:
2948 return typeok_arrow(lt);
2949 case POINT:
2950 return typeok_point(ln, ltp, lt);
2951 case INCBEF:
2952 case DECBEF:
2953 case INCAFT:
2954 case DECAFT:
2955 return typeok_incdec(op, ln, ltp);
2956 case INDIR:
2957 return typeok_indir(ltp, lt);
2958 case ADDR:
2959 return typeok_address(mp, ln, ltp, lt);
2960 case PLUS:
2961 return typeok_plus(op, ltp, lt, rtp, rt);
2962 case MINUS:
2963 return typeok_minus(op, ltp, lt, rtp, rt);
2964 case SHL:
2965 typeok_shl(mp, lt, rt);
2966 goto shift;
2967 case SHR:
2968 typeok_shr(mp, ln, lt, rn, rt);
2969 shift:
2970 typeok_shift(ltp, lt, rn, rt);
2971 break;
2972 case LT:
2973 case LE:
2974 case GT:
2975 case GE:
2976 compare:
2977 return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
2978 case EQ:
2979 case NE:
2980 if (is_typeok_eq(ln, lt, rn, rt))
2981 break;
2982 goto compare;
2983 case QUEST:
2984 return typeok_quest(lt, rn);
2985 case COLON:
2986 return typeok_colon(mp, ln, ltp, lt, rn, rtp, rt);
2987 case ASSIGN:
2988 case INIT:
2989 case FARG:
2990 case RETURN:
2991 if (!check_assign_types_compatible(op, arg, ln, rn))
2992 return false;
2993 goto assign;
2994 case MULASS:
2995 case DIVASS:
2996 case MODASS:
2997 goto assign;
2998 case ADDASS:
2999 case SUBASS:
3000 if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
3001 warn_incompatible_types(op, ltp, lt, rtp, rt);
3002 return false;
3003 }
3004 goto assign;
3005 case SHLASS:
3006 goto assign;
3007 case SHRASS:
3008 if (pflag && !is_uinteger(lt) &&
3009 !(!allow_c90 && is_uinteger(rt))) {
3010 /* bitwise '%s' on signed value possibly nonportable */
3011 warning(117, mp->m_name);
3012 }
3013 goto assign;
3014 case ANDASS:
3015 case XORASS:
3016 case ORASS:
3017 assign:
3018 return typeok_assign(op, ln, ltp, lt);
3019 case COMMA:
3020 if (!modtab[ln->tn_op].m_has_side_effect)
3021 check_null_effect(ln);
3022 break;
3023 default:
3024 break;
3025 }
3026 return true;
3027 }
3028
3029 /* Prints a warning if a strange operator is used on an enum type. */
3030 static void
3031 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
3032 {
3033
3034 if (!eflag)
3035 return;
3036
3037 /*
3038 * Enum as offset to a pointer is an exception (otherwise enums
3039 * could not be used as array indices).
3040 */
3041 if (op == PLUS &&
3042 ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
3043 (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
3044 return;
3045 }
3046
3047 /* dubious operation on enum, op '%s' */
3048 warning(241, op_name(op));
3049 }
3050
3051 /*
3052 * Prints a warning if an operator is applied to two different enum types.
3053 */
3054 static void
3055 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3056 {
3057 const mod_t *mp = &modtab[op];
3058
3059 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
3060 switch (op) {
3061 case INIT:
3062 /* enum type mismatch between '%s' and '%s' in ... */
3063 warning(210,
3064 type_name(ln->tn_type), type_name(rn->tn_type));
3065 break;
3066 case FARG:
3067 /* function expects '%s', passing '%s' for arg #%d */
3068 warning(156,
3069 type_name(ln->tn_type), type_name(rn->tn_type),
3070 arg);
3071 break;
3072 case RETURN:
3073 /* function has return type '%s' but returns '%s' */
3074 warning(211,
3075 type_name(ln->tn_type), type_name(rn->tn_type));
3076 break;
3077 default:
3078 /* enum type mismatch: '%s' '%s' '%s' */
3079 warning(130, type_name(ln->tn_type), mp->m_name,
3080 type_name(rn->tn_type));
3081 break;
3082 }
3083 } else if (Pflag && mp->m_comparison && op != EQ && op != NE) {
3084 if (eflag)
3085 /* dubious comparison of enums, op '%s' */
3086 warning(243, mp->m_name);
3087 }
3088 }
3089
3090 /* Prints a warning if the operands mix between enum and integer. */
3091 static void
3092 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3093 {
3094
3095 if (!eflag)
3096 return;
3097
3098 switch (op) {
3099 case INIT:
3100 /*
3101 * Initialization with 0 is allowed. Otherwise, all implicit
3102 * initializations would need to be warned upon as well.
3103 */
3104 if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
3105 is_integer(rn->tn_type->t_tspec) &&
3106 rn->tn_val->v_quad == 0) {
3107 return;
3108 }
3109 /* initialization of '%s' with '%s' */
3110 warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
3111 break;
3112 case FARG:
3113 /* combination of '%s' and '%s', arg #%d */
3114 warning(278,
3115 type_name(ln->tn_type), type_name(rn->tn_type), arg);
3116 break;
3117 case RETURN:
3118 /* combination of '%s' and '%s' in return */
3119 warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
3120 break;
3121 default:
3122 /* combination of '%s' and '%s', op '%s' */
3123 warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
3124 op_name(op));
3125 break;
3126 }
3127 }
3128
3129 static void
3130 typeok_enum(op_t op, const mod_t *mp, int arg,
3131 const tnode_t *ln, const type_t *ltp,
3132 const tnode_t *rn, const type_t *rtp)
3133 {
3134 if (mp->m_bad_on_enum &&
3135 (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
3136 check_bad_enum_operation(op, ln, rn);
3137 } else if (mp->m_valid_on_enum &&
3138 (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
3139 check_enum_type_mismatch(op, arg, ln, rn);
3140 } else if (mp->m_valid_on_enum &&
3141 (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
3142 check_enum_int_mismatch(op, arg, ln, rn);
3143 }
3144 }
3145
3146 /* Perform most type checks. Return whether the types are ok. */
3147 bool
3148 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3149 {
3150 tspec_t lt, rt;
3151 type_t *ltp, *rtp;
3152
3153 const mod_t *mp = &modtab[op];
3154
3155 lint_assert((ltp = ln->tn_type) != NULL);
3156 lt = ltp->t_tspec;
3157
3158 if (mp->m_binary) {
3159 lint_assert((rtp = rn->tn_type) != NULL);
3160 rt = rtp->t_tspec;
3161 } else {
3162 rtp = NULL;
3163 rt = NOTSPEC;
3164 }
3165
3166 if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
3167 return false;
3168 if (!typeok_scalar(op, mp, ltp, lt, rtp, rt))
3169 return false;
3170
3171 if (!typeok_op(op, mp, arg, ln, ltp, lt, rn, rtp, rt))
3172 return false;
3173
3174 typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
3175 return true;
3176 }
3177
3178 /* In traditional C, keep unsigned and promote FLOAT to DOUBLE. */
3179 static tspec_t
3180 promote_trad(tspec_t t)
3181 {
3182
3183 if (t == UCHAR || t == USHORT)
3184 return UINT;
3185 if (t == CHAR || t == SCHAR || t == SHORT)
3186 return INT;
3187 if (t == FLOAT)
3188 return DOUBLE;
3189 if (t == ENUM)
3190 return INT;
3191 return t;
3192 }
3193
3194 /*
3195 * C99 6.3.1.1p2 requires for types with lower rank than int that "If an int
3196 * can represent all the values of the original type, the value is converted
3197 * to an int; otherwise it is converted to an unsigned int", and that "All
3198 * other types are unchanged by the integer promotions".
3199 */
3200 static tspec_t
3201 promote_c90(const tnode_t *tn, tspec_t t, bool farg)
3202 {
3203 if (tn->tn_type->t_bitfield) {
3204 unsigned int len = tn->tn_type->t_flen;
3205 if (len < size_in_bits(INT))
3206 return INT;
3207 if (len == size_in_bits(INT))
3208 return is_uinteger(t) ? UINT : INT;
3209 return t;
3210 }
3211
3212 if (t == CHAR || t == SCHAR)
3213 return INT;
3214 if (t == UCHAR)
3215 return size_in_bits(CHAR) < size_in_bits(INT) ? INT : UINT;
3216 if (t == SHORT)
3217 return INT;
3218 if (t == USHORT)
3219 return size_in_bits(SHORT) < size_in_bits(INT) ? INT : UINT;
3220 if (t == ENUM)
3221 return INT;
3222 if (farg && t == FLOAT)
3223 return DOUBLE;
3224 return t;
3225 }
3226
3227 /*
3228 * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
3229 * integer types to either int or unsigned int.
3230 *
3231 * If allow_c90 is unset or the operand is a function argument with no type
3232 * information (no prototype or variable # of args), converts float to double.
3233 */
3234 tnode_t *
3235 promote(op_t op, bool farg, tnode_t *tn)
3236 {
3237
3238 tspec_t ot = tn->tn_type->t_tspec;
3239 if (!is_arithmetic(ot))
3240 return tn;
3241
3242 tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot);
3243 if (nt == ot)
3244 return tn;
3245
3246 type_t *ntp = expr_dup_type(tn->tn_type);
3247 ntp->t_tspec = nt;
3248 /*
3249 * Keep t_is_enum even though t_tspec gets converted from
3250 * ENUM to INT, so we are later able to check compatibility
3251 * of enum types.
3252 */
3253 return convert(op, 0, ntp, tn);
3254 }
3255
3256 static void
3257 convert_integer_from_floating(op_t op, const type_t *tp, const tnode_t *tn)
3258 {
3259
3260 if (op == CVT)
3261 /* cast from floating point '%s' to integer '%s' */
3262 query_message(2, type_name(tn->tn_type), type_name(tp));
3263 else
3264 /* implicit conversion from floating point '%s' to ... */
3265 query_message(1, type_name(tn->tn_type), type_name(tp));
3266 }
3267
3268 static bool
3269 should_warn_about_prototype_conversion(tspec_t nt,
3270 tspec_t ot, const tnode_t *ptn)
3271 {
3272
3273 if (nt == ot)
3274 return false;
3275
3276 if (nt == ENUM && ot == INT)
3277 return false;
3278
3279 if (is_floating(nt) != is_floating(ot) ||
3280 portable_size_in_bits(nt) != portable_size_in_bits(ot)) {
3281 /* representation and/or width change */
3282 if (!is_integer(ot))
3283 return true;
3284 /*
3285 * XXX: Investigate whether this rule makes sense; see
3286 * tests/usr.bin/xlint/lint1/platform_long.c.
3287 */
3288 return portable_size_in_bits(ot) > portable_size_in_bits(INT);
3289 }
3290
3291 if (!hflag)
3292 return false;
3293
3294 /*
3295 * If the types differ only in sign and the argument has the same
3296 * representation in both types, print no warning.
3297 */
3298 if (ptn->tn_op == CON && is_integer(nt) &&
3299 signed_type(nt) == signed_type(ot) &&
3300 !msb(ptn->tn_val->v_quad, ot))
3301 return false;
3302
3303 return true;
3304 }
3305
3306 /*
3307 * Warn if a prototype causes a type conversion that is different from what
3308 * would happen to the same argument in the absence of a prototype. This
3309 * check is intended for code that needs to stay compatible with pre-C90 C.
3310 *
3311 * Errors/warnings about illegal type combinations are already printed
3312 * in check_assign_types_compatible().
3313 */
3314 static void
3315 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
3316 tnode_t *tn)
3317 {
3318
3319 if (!is_arithmetic(nt) || !is_arithmetic(ot))
3320 return;
3321
3322 /*
3323 * If the type of the formal parameter is char/short, a warning
3324 * would be useless, because functions declared the old style
3325 * can't expect char/short arguments.
3326 */
3327 if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
3328 nt == SHORT || nt == USHORT)
3329 return;
3330
3331 /* apply the default promotion */
3332 tnode_t *ptn = promote(NOOP, true, tn);
3333 ot = ptn->tn_type->t_tspec;
3334
3335 if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
3336 /* argument #%d is converted from '%s' to '%s' ... */
3337 warning(259, arg, type_name(tn->tn_type), type_name(tp));
3338 }
3339 }
3340
3341 /*
3342 * When converting a large integer type to a small integer type, in some
3343 * cases the value of the actual expression is further restricted than the
3344 * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24).
3345 */
3346 static bool
3347 can_represent(const type_t *tp, const tnode_t *tn)
3348 {
3349
3350 debug_step("%s: type '%s'", __func__, type_name(tp));
3351 debug_node(tn);
3352
3353 uint64_t nmask = value_bits(width_in_bits(tp));
3354 if (!is_uinteger(tp->t_tspec))
3355 nmask >>= 1;
3356
3357 integer_constraints c = ic_expr(tn);
3358 if ((~c.bclr & ~nmask) == 0)
3359 return true;
3360
3361 return false;
3362 }
3363
3364 static void
3365 convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
3366 type_t *tp, tnode_t *tn)
3367 {
3368
3369 if (tn->tn_op == CON)
3370 return;
3371
3372 if (op == CVT)
3373 return;
3374
3375 if (Pflag && pflag && aflag > 0 &&
3376 portable_size_in_bits(nt) > portable_size_in_bits(ot) &&
3377 is_uinteger(nt) != is_uinteger(ot)) {
3378 if (op == FARG) {
3379 /* conversion to '%s' may sign-extend ... */
3380 warning(297, type_name(tp), arg);
3381 } else {
3382 /* conversion to '%s' may sign-extend ... */
3383 warning(131, type_name(tp));
3384 }
3385 }
3386
3387 if (Pflag && portable_size_in_bits(nt) > portable_size_in_bits(ot) &&
3388 (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
3389 tn->tn_op == SHL)) {
3390 /* suggest cast from '%s' to '%s' on op '%s' to ... */
3391 warning(324, type_name(gettyp(ot)), type_name(tp),
3392 op_name(tn->tn_op));
3393 }
3394
3395 if (aflag > 0 &&
3396 portable_size_in_bits(nt) < portable_size_in_bits(ot) &&
3397 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
3398 aflag > 1) &&
3399 !can_represent(tp, tn)) {
3400 if (op == FARG) {
3401 /* conversion from '%s' to '%s' may lose ... */
3402 warning(298,
3403 type_name(tn->tn_type), type_name(tp), arg);
3404 } else {
3405 /* conversion from '%s' to '%s' may lose accuracy */
3406 warning(132,
3407 type_name(tn->tn_type), type_name(tp));
3408 }
3409 }
3410
3411 if (is_uinteger(nt) != is_uinteger(ot))
3412 /* implicit conversion changes sign from '%s' to '%s' */
3413 query_message(3, type_name(tn->tn_type), type_name(tp));
3414 }
3415
3416 static void
3417 convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
3418 {
3419
3420 if (tn->tn_op == CON)
3421 return;
3422 if (op != CVT)
3423 return; /* We already got an error. */
3424 if (portable_size_in_bits(nt) >= portable_size_in_bits(PTR))
3425 return;
3426
3427 if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
3428 /* conversion of pointer to '%s' may lose bits */
3429 warning(134, type_name(tp));
3430 } else {
3431 /* conversion of pointer to '%s' loses bits */
3432 warning(133, type_name(tp));
3433 }
3434 }
3435
3436 static bool
3437 struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
3438 {
3439
3440 return struct_tp->t_sou->sou_first_member != NULL &&
3441 types_compatible(struct_tp->t_sou->sou_first_member->s_type,
3442 member_tp, true, false, NULL);
3443 }
3444
3445 static bool
3446 is_byte_array(const type_t *tp)
3447 {
3448
3449 return tp->t_tspec == ARRAY &&
3450 (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
3451 }
3452
3453 static bool
3454 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
3455 const type_t *ostp, tspec_t ost)
3456 {
3457
3458 while (nst == ARRAY)
3459 nstp = nstp->t_subt, nst = nstp->t_tspec;
3460 while (ost == ARRAY)
3461 ostp = ostp->t_subt, ost = ostp->t_tspec;
3462
3463 if (nst == STRUCT && ost == STRUCT &&
3464 (struct_starts_with(nstp, ostp) ||
3465 struct_starts_with(ostp, nstp)))
3466 return false;
3467
3468 if (is_incomplete(nstp) || is_incomplete(ostp))
3469 return false;
3470
3471 if (nst == CHAR || nst == UCHAR)
3472 return false; /* for the sake of traditional C code */
3473 if (ost == CHAR || ost == UCHAR)
3474 return false; /* for the sake of traditional C code */
3475
3476 /* Allow cast between pointers to sockaddr variants. */
3477 if (nst == STRUCT && ost == STRUCT) {
3478 debug_type(nstp);
3479 debug_type(ostp);
3480 const sym_t *nmem = nstp->t_sou->sou_first_member;
3481 const sym_t *omem = ostp->t_sou->sou_first_member;
3482 while (nmem != NULL && omem != NULL &&
3483 types_compatible(nmem->s_type, omem->s_type,
3484 true, false, NULL))
3485 nmem = nmem->s_next, omem = omem->s_next;
3486 if (nmem != NULL && is_byte_array(nmem->s_type))
3487 return false;
3488 if (omem != NULL && is_byte_array(omem->s_type))
3489 return false;
3490 if (nmem == NULL && omem == NULL)
3491 return false;
3492 }
3493
3494 if (nst == UNION || ost == UNION) {
3495 const type_t *union_tp = nst == UNION ? nstp : ostp;
3496 const type_t *other_tp = nst == UNION ? ostp : nstp;
3497 for (const sym_t *mem = union_tp->t_sou->sou_first_member;
3498 mem != NULL; mem = mem->s_next) {
3499 if (types_compatible(mem->s_type, other_tp,
3500 true, false, NULL))
3501 return false;
3502 }
3503 }
3504
3505 if (is_struct_or_union(nst) && nstp->t_sou != ostp->t_sou)
3506 return true;
3507
3508 return portable_size_in_bits(nst) != portable_size_in_bits(ost);
3509 }
3510
3511 static void
3512 convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
3513 {
3514 const type_t *nstp = ntp->t_subt;
3515 const type_t *otp = tn->tn_type;
3516 const type_t *ostp = otp->t_subt;
3517 tspec_t nst = nstp->t_tspec;
3518 tspec_t ost = ostp->t_tspec;
3519
3520 if (nst == VOID || ost == VOID) {
3521 /* TODO: C99 behaves like C90 here. */
3522 if ((!allow_trad && !allow_c99) && (nst == FUNC || ost == FUNC)) {
3523 const char *nts, *ots;
3524 /* null pointers are already handled in convert() */
3525 *(nst == FUNC ? &nts : &ots) = "function pointer";
3526 *(nst == VOID ? &nts : &ots) = "'void *'";
3527 /* ANSI C forbids conversion of %s to %s */
3528 warning(303, ots, nts);
3529 }
3530 return;
3531 }
3532 if (nst == FUNC && ost == FUNC)
3533 return;
3534 if (nst == FUNC || ost == FUNC) {
3535 /* converting '%s' to '%s' is questionable */
3536 warning(229, type_name(otp), type_name(ntp));
3537 return;
3538 }
3539
3540 if (hflag && alignment_in_bits(nstp) > alignment_in_bits(ostp) &&
3541 ost != CHAR && ost != UCHAR &&
3542 !is_incomplete(ostp)) {
3543 /* converting '%s' to '%s' increases alignment ... */
3544 warning(135, type_name(otp), type_name(ntp),
3545 alignment_in_bits(ostp) / CHAR_SIZE,
3546 alignment_in_bits(nstp) / CHAR_SIZE);
3547 }
3548
3549 if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
3550 /* pointer cast from '%s' to '%s' may be troublesome */
3551 warning(247, type_name(otp), type_name(ntp));
3552 }
3553 }
3554
3555 /*
3556 * Insert a conversion operator, which converts the type of the node
3557 * to another given type.
3558 *
3559 * Possible values for 'op':
3560 * CVT a cast-expression
3561 * binary integer promotion for one of the operands, or a usual
3562 * arithmetic conversion
3563 * binary plain or compound assignments to bit-fields
3564 * FARG 'arg' is the number of the argument (used for warnings)
3565 * NOOP several other implicit conversions
3566 * ...
3567 */
3568 tnode_t *
3569 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
3570 {
3571 tspec_t nt = tp->t_tspec;
3572 tspec_t ot = tn->tn_type->t_tspec;
3573
3574 if (allow_trad && allow_c90 && op == FARG)
3575 check_prototype_conversion(arg, nt, ot, tp, tn);
3576
3577 if (nt == BOOL) {
3578 /* No further checks. */
3579
3580 } else if (is_integer(nt)) {
3581 if (ot == BOOL) {
3582 /* No further checks. */
3583 } else if (is_integer(ot)) {
3584 convert_integer_from_integer(op, arg, nt, ot, tp, tn);
3585 } else if (is_floating(ot)) {
3586 convert_integer_from_floating(op, tp, tn);
3587 } else if (ot == PTR) {
3588 convert_integer_from_pointer(op, nt, tp, tn);
3589 }
3590
3591 } else if (is_floating(nt)) {
3592 /* No further checks. */
3593
3594 } else if (nt == PTR) {
3595 if (is_null_pointer(tn)) {
3596 /* a null pointer may be assigned to any pointer. */
3597 } else if (ot == PTR && op == CVT) {
3598 convert_pointer_from_pointer(tp, tn);
3599 }
3600 }
3601
3602 tnode_t *ntn = expr_alloc_tnode();
3603 ntn->tn_op = CVT;
3604 ntn->tn_type = tp;
3605 ntn->tn_cast = op == CVT;
3606 ntn->tn_sys |= tn->tn_sys;
3607 ntn->tn_right = NULL;
3608 if (tn->tn_op != CON || nt == VOID) {
3609 ntn->tn_left = tn;
3610 } else {
3611 ntn->tn_op = CON;
3612 ntn->tn_val = expr_zero_alloc(sizeof(*ntn->tn_val));
3613 convert_constant(op, arg, ntn->tn_type, ntn->tn_val,
3614 tn->tn_val);
3615 }
3616
3617 return ntn;
3618 }
3619
3620 static void
3621 convert_constant_floating(op_t op, int arg, tspec_t ot, const type_t *tp,
3622 tspec_t nt, val_t *v, val_t *nv)
3623 {
3624 ldbl_t max = 0.0, min = 0.0;
3625
3626 switch (nt) {
3627 case CHAR:
3628 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
3629 case UCHAR:
3630 max = TARG_UCHAR_MAX; min = 0; break;
3631 case SCHAR:
3632 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
3633 case SHORT:
3634 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
3635 case USHORT:
3636 max = TARG_USHRT_MAX; min = 0; break;
3637 case ENUM:
3638 case INT:
3639 max = TARG_INT_MAX; min = TARG_INT_MIN; break;
3640 case UINT:
3641 max = TARG_UINT_MAX; min = 0; break;
3642 case LONG:
3643 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
3644 case ULONG:
3645 max = TARG_ULONG_MAX; min = 0; break;
3646 case QUAD:
3647 max = QUAD_MAX; min = QUAD_MIN; break;
3648 case UQUAD:
3649 max = UQUAD_MAX; min = 0; break;
3650 case FLOAT:
3651 case FCOMPLEX:
3652 max = FLT_MAX; min = -FLT_MAX; break;
3653 case DOUBLE:
3654 case DCOMPLEX:
3655 max = DBL_MAX; min = -DBL_MAX; break;
3656 case PTR:
3657 /* Already got an error because of float --> ptr */
3658 case LDOUBLE:
3659 case LCOMPLEX:
3660 /* LINTED 248 */
3661 max = LDBL_MAX; min = -max; break;
3662 default:
3663 lint_assert(/*CONSTCOND*/false);
3664 }
3665 if (v->v_ldbl > max || v->v_ldbl < min) {
3666 lint_assert(nt != LDOUBLE);
3667 if (op == FARG) {
3668 /* conversion of '%s' to '%s' is out of range, ... */
3669 warning(295,
3670 type_name(gettyp(ot)), type_name(tp), arg);
3671 } else {
3672 /* conversion of '%s' to '%s' is out of range */
3673 warning(119,
3674 type_name(gettyp(ot)), type_name(tp));
3675 }
3676 v->v_ldbl = v->v_ldbl > 0 ? max : min;
3677 }
3678
3679 if (nt == FLOAT || nt == FCOMPLEX) {
3680 nv->v_ldbl = (float)v->v_ldbl;
3681 } else if (nt == DOUBLE || nt == DCOMPLEX) {
3682 nv->v_ldbl = (double)v->v_ldbl;
3683 } else if (nt == LDOUBLE || nt == LCOMPLEX) {
3684 nv->v_ldbl = v->v_ldbl;
3685 } else {
3686 nv->v_quad = (int64_t)v->v_ldbl;
3687 }
3688 }
3689
3690 static bool
3691 convert_constant_to_floating(tspec_t nt, val_t *nv,
3692 tspec_t ot, const val_t *v)
3693 {
3694 if (nt == FLOAT) {
3695 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
3696 (float)(uint64_t)v->v_quad : (float)v->v_quad;
3697 } else if (nt == DOUBLE) {
3698 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
3699 (double)(uint64_t)v->v_quad : (double)v->v_quad;
3700 } else if (nt == LDOUBLE) {
3701 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
3702 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
3703 } else
3704 return false;
3705 return true;
3706 }
3707
3708 /*
3709 * Print a warning if bits which were set are lost due to the conversion.
3710 * This can happen with operator ORASS only.
3711 */
3712 static void
3713 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
3714 uint64_t xmask, op_t op)
3715 {
3716 if (nsz < osz && (v->v_quad & xmask) != 0) {
3717 /* constant truncated by conversion, op '%s' */
3718 warning(306, op_name(op));
3719 }
3720 }
3721
3722 /*
3723 * Print a warning if additional bits are not all 1
3724 * and the most significant bit of the old value is 1,
3725 * or if at least one (but not all) removed bit was 0.
3726 */
3727 static void
3728 convert_constant_check_range_bitand(size_t nsz, size_t osz,
3729 uint64_t xmask, const val_t *nv,
3730 tspec_t ot, const val_t *v,
3731 const type_t *tp, op_t op)
3732 {
3733 if (nsz > osz &&
3734 (nv->v_quad & bit((unsigned int)(osz - 1))) != 0 &&
3735 (nv->v_quad & xmask) != xmask) {
3736 /* extra bits set to 0 in conversion of '%s' to '%s', ... */
3737 warning(309, type_name(gettyp(ot)),
3738 type_name(tp), op_name(op));
3739 } else if (nsz < osz &&
3740 (v->v_quad & xmask) != xmask &&
3741 (v->v_quad & xmask) != 0) {
3742 /* constant truncated by conversion, op '%s' */
3743 warning(306, op_name(op));
3744 }
3745 }
3746
3747 static void
3748 convert_constant_check_range_signed(op_t op, int arg)
3749 {
3750 if (op == ASSIGN) {
3751 /* assignment of negative constant to unsigned type */
3752 warning(164);
3753 } else if (op == INIT) {
3754 /* initialization of unsigned with negative constant */
3755 warning(221);
3756 } else if (op == FARG) {
3757 /* conversion of negative constant to unsigned type, ... */
3758 warning(296, arg);
3759 } else if (modtab[op].m_comparison) {
3760 /* handled by check_integer_comparison() */
3761 } else {
3762 /* conversion of negative constant to unsigned type */
3763 warning(222);
3764 }
3765 }
3766
3767 /*
3768 * Loss of significant bit(s). All truncated bits of unsigned types or all
3769 * truncated bits plus the msb of the target for signed types are considered
3770 * to be significant bits. Loss of significant bits means that at least one
3771 * of the bits was set in an unsigned type or that at least one but not all
3772 * of the bits was set in a signed type. Loss of significant bits means that
3773 * it is not possible, also not with necessary casts, to convert back to the
3774 * original type. A example for a necessary cast is:
3775 * char c; int i; c = 128;
3776 * i = c; ** yields -128 **
3777 * i = (unsigned char)c; ** yields 128 **
3778 */
3779 static void
3780 convert_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
3781 tspec_t ot)
3782 {
3783 if (op == ASSIGN && tp->t_bitfield) {
3784 /* precision lost in bit-field assignment */
3785 warning(166);
3786 } else if (op == ASSIGN) {
3787 /* constant truncated by assignment */
3788 warning(165);
3789 } else if (op == INIT && tp->t_bitfield) {
3790 /* bit-field initializer does not fit */
3791 warning(180);
3792 } else if (op == INIT) {
3793 /* initializer does not fit */
3794 warning(178);
3795 } else if (op == CASE) {
3796 /* case label affected by conversion */
3797 warning(196);
3798 } else if (op == FARG) {
3799 /* conversion of '%s' to '%s' is out of range, arg #%d */
3800 warning(295,
3801 type_name(gettyp(ot)), type_name(tp), arg);
3802 } else {
3803 /* conversion of '%s' to '%s' is out of range */
3804 warning(119,
3805 type_name(gettyp(ot)), type_name(tp));
3806 }
3807 }
3808
3809 static void
3810 convert_constant_check_range_loss(op_t op, int arg, const type_t *tp,
3811 tspec_t ot)
3812 {
3813 if (op == ASSIGN && tp->t_bitfield) {
3814 /* precision lost in bit-field assignment */
3815 warning(166);
3816 } else if (op == INIT && tp->t_bitfield) {
3817 /* bit-field initializer out of range */
3818 warning(11);
3819 } else if (op == CASE) {
3820 /* case label affected by conversion */
3821 warning(196);
3822 } else if (op == FARG) {
3823 /* conversion of '%s' to '%s' is out of range, arg #%d */
3824 warning(295, type_name(gettyp(ot)), type_name(tp), arg);
3825 } else {
3826 /* conversion of '%s' to '%s' is out of range */
3827 warning(119, type_name(gettyp(ot)), type_name(tp));
3828 }
3829 }
3830
3831 static void
3832 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
3833 op_t op, int arg, const val_t *v, val_t *nv)
3834 {
3835 unsigned int obitsz, nbitsz;
3836 uint64_t xmask, xmsk1;
3837
3838 obitsz = size_in_bits(ot);
3839 nbitsz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt);
3840 xmask = value_bits(nbitsz) ^ value_bits(obitsz);
3841 xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
3842 /*
3843 * For bitwise operations we are not interested in the arithmetic
3844 * value, but in the bits itself.
3845 */
3846 if (op == ORASS || op == BITOR || op == BITXOR) {
3847 convert_constant_check_range_bitor(
3848 nbitsz, obitsz, v, xmask, op);
3849 } else if (op == ANDASS || op == BITAND) {
3850 convert_constant_check_range_bitand(
3851 nbitsz, obitsz, xmask, nv, ot, v, tp, op);
3852 } else if ((nt != PTR && is_uinteger(nt)) &&
3853 (ot != PTR && !is_uinteger(ot)) &&
3854 v->v_quad < 0) {
3855 convert_constant_check_range_signed(op, arg);
3856 } else if (nv->v_quad != v->v_quad && nbitsz <= obitsz &&
3857 (v->v_quad & xmask) != 0 &&
3858 (is_uinteger(ot) || (v->v_quad & xmsk1) != xmsk1)) {
3859 convert_constant_check_range_truncated(op, arg, tp, ot);
3860 } else if (nv->v_quad != v->v_quad) {
3861 convert_constant_check_range_loss(op, arg, tp, ot);
3862 }
3863 }
3864
3865 /*
3866 * Converts a typed constant to a constant of another type.
3867 *
3868 * op operator which requires conversion
3869 * arg if op is FARG, # of argument
3870 * tp type to which to convert the constant
3871 * nv new constant
3872 * v old constant
3873 */
3874 void
3875 convert_constant(op_t op, int arg, const type_t *tp, val_t *nv, val_t *v)
3876 {
3877 /*
3878 * TODO: make 'v' const; the name of this function does not suggest
3879 * that it modifies 'v'.
3880 */
3881 tspec_t ot = v->v_tspec;
3882 tspec_t nt = nv->v_tspec = tp->t_tspec;
3883 bool range_check = false;
3884
3885 if (nt == BOOL) { /* C99 6.3.1.2 */
3886 nv->v_unsigned_since_c90 = false;
3887 nv->v_quad = is_nonzero_val(v) ? 1 : 0;
3888 return;
3889 }
3890
3891 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
3892 convert_constant_floating(op, arg, ot, tp, nt, v, nv);
3893 } else if (!convert_constant_to_floating(nt, nv, ot, v)) {
3894 range_check = true; /* Check for lost precision. */
3895 nv->v_quad = v->v_quad;
3896 }
3897
3898 if (allow_trad && allow_c90 && v->v_unsigned_since_c90 &&
3899 (is_floating(nt) || (
3900 (is_integer(nt) && !is_uinteger(nt) &&
3901 portable_size_in_bits(nt) > portable_size_in_bits(ot))))) {
3902 /* ANSI C treats constant as unsigned */
3903 warning(157);
3904 v->v_unsigned_since_c90 = false;
3905 }
3906
3907 if (is_integer(nt)) {
3908 nv->v_quad = convert_integer(nv->v_quad, nt,
3909 tp->t_bitfield ? tp->t_flen : size_in_bits(nt));
3910 }
3911
3912 if (range_check && op != CVT)
3913 convert_constant_check_range(ot, tp, nt, op, arg, v, nv);
3914 }
3915
3916 /*
3917 * Create a constant node for sizeof.
3918 */
3919 tnode_t *
3920 build_sizeof(const type_t *tp)
3921 {
3922 unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
3923 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
3924 tn->tn_system_dependent = true;
3925 debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
3926 return tn;
3927 }
3928
3929 /*
3930 * Create a constant node for offsetof.
3931 */
3932 /* ARGSUSED */ /* FIXME: See implementation comments. */
3933 tnode_t *
3934 build_offsetof(const type_t *tp, const sym_t *sym)
3935 {
3936 unsigned int offset_in_bytes;
3937 tnode_t *tn;
3938
3939 if (!is_struct_or_union(tp->t_tspec))
3940 /* unacceptable operand of '%s' */
3941 error(111, "offsetof");
3942
3943 /* FIXME: Don't wrongly use the size of the whole type, use sym. */
3944 offset_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
3945 tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
3946 tn->tn_system_dependent = true;
3947 return tn;
3948 }
3949
3950 unsigned int
3951 type_size_in_bits(const type_t *tp)
3952 {
3953 unsigned int elsz;
3954
3955 unsigned int elem = 1;
3956 bool flex = false;
3957 lint_assert(tp != NULL);
3958 while (tp->t_tspec == ARRAY) {
3959 flex = true; /* allow c99 flex arrays [] [0] */
3960 elem *= tp->t_dim;
3961 tp = tp->t_subt;
3962 }
3963 if (elem == 0) {
3964 if (!flex) {
3965 /* cannot take size/alignment of incomplete type */
3966 error(143);
3967 elem = 1;
3968 }
3969 }
3970 switch (tp->t_tspec) {
3971 case FUNC:
3972 /* cannot take size/alignment of function type '%s' */
3973 error(144, type_name(tp));
3974 elsz = 1;
3975 break;
3976 case STRUCT:
3977 case UNION:
3978 if (is_incomplete(tp)) {
3979 /* cannot take size/alignment of incomplete type */
3980 error(143);
3981 elsz = 1;
3982 } else {
3983 elsz = tp->t_sou->sou_size_in_bits;
3984 }
3985 break;
3986 case ENUM:
3987 if (is_incomplete(tp)) {
3988 /* cannot take size/alignment of incomplete type */
3989 warning(143);
3990 }
3991 /* FALLTHROUGH */
3992 default:
3993 if (tp->t_bitfield) {
3994 /* cannot take size/alignment of bit-field */
3995 error(145);
3996 }
3997 if (tp->t_tspec == VOID) {
3998 /* cannot take size/alignment of void */
3999 error(146);
4000 elsz = 1;
4001 } else {
4002 elsz = size_in_bits(tp->t_tspec);
4003 lint_assert(elsz > 0);
4004 }
4005 break;
4006 }
4007
4008 return elem * elsz;
4009 }
4010
4011 tnode_t *
4012 build_alignof(const type_t *tp)
4013 {
4014 switch (tp->t_tspec) {
4015 case ARRAY:
4016 break;
4017
4018 case FUNC:
4019 /* cannot take size/alignment of function type '%s' */
4020 error(144, type_name(tp));
4021 return 0;
4022
4023 case STRUCT:
4024 case UNION:
4025 if (is_incomplete(tp)) {
4026 /* cannot take size/alignment of incomplete type */
4027 error(143);
4028 return 0;
4029 }
4030 break;
4031 case ENUM:
4032 break;
4033 default:
4034 if (tp->t_bitfield) {
4035 /* cannot take size/alignment of bit-field */
4036 error(145);
4037 return 0;
4038 }
4039 if (tp->t_tspec == VOID) {
4040 /* cannot take size/alignment of void */
4041 error(146);
4042 return 0;
4043 }
4044 break;
4045 }
4046
4047 return build_integer_constant(SIZEOF_TSPEC,
4048 (int64_t)alignment_in_bits(tp) / CHAR_SIZE);
4049 }
4050
4051 static tnode_t *
4052 cast_to_union(const tnode_t *otn, type_t *ntp)
4053 {
4054
4055 if (!allow_gcc) {
4056 /* union cast is a GCC extension */
4057 error(328);
4058 return NULL;
4059 }
4060
4061 for (const sym_t *m = ntp->t_sou->sou_first_member;
4062 m != NULL; m = m->s_next) {
4063 if (types_compatible(m->s_type, otn->tn_type,
4064 false, false, NULL)) {
4065 tnode_t *ntn = expr_alloc_tnode();
4066 ntn->tn_op = CVT;
4067 ntn->tn_type = ntp;
4068 ntn->tn_cast = true;
4069 ntn->tn_right = NULL;
4070 return ntn;
4071 }
4072 }
4073
4074 /* type '%s' is not a member of '%s' */
4075 error(329, type_name(otn->tn_type), type_name(ntp));
4076 return NULL;
4077 }
4078
4079 /*
4080 * Type casts.
4081 */
4082 tnode_t *
4083 cast(tnode_t *tn, type_t *tp)
4084 {
4085 tspec_t nt, ot;
4086
4087 if (tn == NULL)
4088 return NULL;
4089
4090 tn = cconv(tn);
4091
4092 lint_assert(tp != NULL);
4093 nt = tp->t_tspec;
4094 ot = tn->tn_type->t_tspec;
4095
4096 if (nt == VOID) {
4097 /*
4098 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to
4099 * be cast to void. The only other allowed casts are from a
4100 * scalar type to a scalar type.
4101 */
4102 } else if (nt == UNION) {
4103 return cast_to_union(tn, tp);
4104 } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
4105 /* Casting to a struct is an undocumented GCC extension. */
4106 if (!(allow_gcc && nt == STRUCT))
4107 goto invalid_cast;
4108 } else if (is_struct_or_union(ot)) {
4109 goto invalid_cast;
4110 } else if (ot == VOID) {
4111 /* improper cast of void expression */
4112 error(148);
4113 return NULL;
4114 } else if (is_integer(nt) && is_scalar(ot)) {
4115 /* ok */
4116 } else if (is_floating(nt) && is_arithmetic(ot)) {
4117 /* ok */
4118 } else if (nt == PTR && is_integer(ot)) {
4119 /* ok */
4120 } else if (nt == PTR && ot == PTR) {
4121 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
4122 if (hflag)
4123 /* cast discards 'const' from type '%s' */
4124 warning(275, type_name(tn->tn_type));
4125 }
4126 } else
4127 goto invalid_cast;
4128
4129 if (any_query_enabled && types_compatible(tp, tn->tn_type,
4130 false, false, NULL)) {
4131 /* no-op cast from '%s' to '%s' */
4132 query_message(6, type_name(tn->tn_type), type_name(tp));
4133 }
4134
4135 tn = convert(CVT, 0, tp, tn);
4136 tn->tn_cast = true;
4137
4138 return tn;
4139
4140 invalid_cast:
4141 /* invalid cast from '%s' to '%s' */
4142 error(147, type_name(tn->tn_type), type_name(tp));
4143 return NULL;
4144 }
4145
4146 /*
4147 * Create the node for a function argument.
4148 * All necessary conversions and type checks are done in
4149 * build_function_call because build_function_argument has no
4150 * information about expected argument types.
4151 */
4152 tnode_t *
4153 build_function_argument(tnode_t *args, tnode_t *arg)
4154 {
4155 /*
4156 * If there was a serious error in the expression for the argument,
4157 * create a dummy argument so the positions of the remaining arguments
4158 * will not change.
4159 */
4160 if (arg == NULL)
4161 arg = build_integer_constant(INT, 0);
4162
4163 return new_tnode(PUSH, arg->tn_sys, arg->tn_type, arg, args);
4164 }
4165
4166 /*
4167 * Compare the type of an argument with the corresponding type of a
4168 * prototype parameter. If it is a valid combination, but both types
4169 * are not the same, insert a conversion to convert the argument into
4170 * the type of the parameter.
4171 */
4172 static tnode_t *
4173 check_prototype_argument(
4174 int n, /* pos of arg */
4175 type_t *tp, /* expected type (from prototype) */
4176 tnode_t *tn) /* argument */
4177 {
4178 tnode_t *ln = xcalloc(1, sizeof(*ln));
4179 ln->tn_type = expr_unqualified_type(tp);
4180 ln->tn_lvalue = true;
4181 if (typeok(FARG, n, ln, tn)) {
4182 bool dowarn;
4183 if (!types_compatible(tp, tn->tn_type,
4184 true, false, (dowarn = false, &dowarn)) || dowarn)
4185 tn = convert(FARG, n, tp, tn);
4186 }
4187 free(ln);
4188 return tn;
4189 }
4190
4191 /*
4192 * Check types of all function arguments and insert conversions,
4193 * if necessary.
4194 */
4195 static tnode_t *
4196 check_function_arguments(type_t *ftp, tnode_t *args)
4197 {
4198 tnode_t *arg;
4199 sym_t *asym;
4200 tspec_t at;
4201 int narg, npar, n, i;
4202
4203 /* get # of args in the prototype */
4204 npar = 0;
4205 for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
4206 npar++;
4207
4208 /* get # of args in function call */
4209 narg = 0;
4210 for (arg = args; arg != NULL; arg = arg->tn_right)
4211 narg++;
4212
4213 asym = ftp->t_args;
4214 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
4215 /* argument mismatch: %d %s passed, %d expected */
4216 error(150, narg, narg > 1 ? "arguments" : "argument", npar);
4217 asym = NULL;
4218 }
4219
4220 for (n = 1; n <= narg; n++) {
4221
4222 /*
4223 * The rightmost argument is at the top of the argument
4224 * subtree.
4225 */
4226 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
4227 continue;
4228
4229 /* some things which are always not allowed */
4230 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
4231 /* void expressions may not be arguments, arg #%d */
4232 error(151, n);
4233 return NULL;
4234 } else if (is_struct_or_union(at) &&
4235 is_incomplete(arg->tn_left->tn_type)) {
4236 /* argument cannot have unknown size, arg #%d */
4237 error(152, n);
4238 return NULL;
4239 } else if (is_integer(at) &&
4240 arg->tn_left->tn_type->t_is_enum &&
4241 is_incomplete(arg->tn_left->tn_type)) {
4242 /* argument cannot have unknown size, arg #%d */
4243 warning(152, n);
4244 }
4245
4246 /* class conversions (arg in value context) */
4247 arg->tn_left = cconv(arg->tn_left);
4248
4249 if (asym != NULL) {
4250 arg->tn_left = check_prototype_argument(
4251 n, asym->s_type, arg->tn_left);
4252 } else {
4253 arg->tn_left = promote(NOOP, true, arg->tn_left);
4254 }
4255 arg->tn_type = arg->tn_left->tn_type;
4256
4257 if (asym != NULL)
4258 asym = asym->s_next;
4259 }
4260
4261 return args;
4262 }
4263
4264 /*
4265 * Create the node for a function call. Also check types of
4266 * function arguments and insert conversions, if necessary.
4267 */
4268 tnode_t *
4269 build_function_call(tnode_t *func, bool sys, tnode_t *args)
4270 {
4271 tnode_t *ntn;
4272 op_t fcop;
4273
4274 if (func == NULL)
4275 return NULL;
4276
4277 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
4278 fcop = CALL;
4279 } else {
4280 fcop = ICALL;
4281 }
4282
4283 check_ctype_function_call(func, args);
4284
4285 /*
4286 * after cconv() func will always be a pointer to a function
4287 * if it is a valid function designator.
4288 */
4289 func = cconv(func);
4290
4291 if (func->tn_type->t_tspec != PTR ||
4292 func->tn_type->t_subt->t_tspec != FUNC) {
4293 /* cannot call '%s', must be a function */
4294 error(149, type_name(func->tn_type));
4295 return NULL;
4296 }
4297
4298 args = check_function_arguments(func->tn_type->t_subt, args);
4299
4300 ntn = new_tnode(fcop, sys, func->tn_type->t_subt->t_subt, func, args);
4301
4302 return ntn;
4303 }
4304
4305 /*
4306 * Return the value of an integral constant expression.
4307 * If the expression is not constant or its type is not an integer
4308 * type, an error message is printed.
4309 */
4310 val_t *
4311 constant(tnode_t *tn, bool required)
4312 {
4313
4314 if (tn != NULL)
4315 tn = cconv(tn);
4316 if (tn != NULL)
4317 tn = promote(NOOP, false, tn);
4318
4319 val_t *v = xcalloc(1, sizeof(*v));
4320
4321 if (tn == NULL) {
4322 lint_assert(nerr != 0);
4323 debug_step("constant node is null; returning 1 instead");
4324 v->v_tspec = INT;
4325 v->v_quad = 1;
4326 return v;
4327 }
4328
4329 v->v_tspec = tn->tn_type->t_tspec;
4330
4331 if (tn->tn_op == CON) {
4332 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
4333 if (is_integer(tn->tn_val->v_tspec)) {
4334 v->v_unsigned_since_c90 =
4335 tn->tn_val->v_unsigned_since_c90;
4336 v->v_quad = tn->tn_val->v_quad;
4337 return v;
4338 }
4339 v->v_quad = tn->tn_val->v_ldbl;
4340 } else {
4341 v->v_quad = 1;
4342 }
4343
4344 if (required)
4345 /* integral constant expression expected */
4346 error(55);
4347 else
4348 /* variable array dimension is a C99/GCC extension */
4349 c99ism(318);
4350
4351 if (!is_integer(v->v_tspec))
4352 v->v_tspec = INT;
4353
4354 return v;
4355 }
4356
4357 static bool
4358 is_constcond_false(const tnode_t *tn, tspec_t t)
4359 {
4360 return (t == BOOL || t == INT) &&
4361 tn->tn_op == CON && tn->tn_val->v_quad == 0;
4362 }
4363
4364 /*
4365 * Perform some tests on expressions which can't be done in build_binary()
4366 * and functions called by build_binary(). These tests must be done here
4367 * because we need some information about the context in which the operations
4368 * are performed.
4369 * After all tests are performed and dofreeblk is true, expr() frees the
4370 * memory which is used for the expression.
4371 */
4372 void
4373 expr(tnode_t *tn, bool vctx, bool cond, bool dofreeblk, bool is_do_while)
4374 {
4375
4376 if (tn == NULL) { /* in case of errors */
4377 expr_free_all();
4378 return;
4379 }
4380
4381 /* expr() is also called in global initializations */
4382 if (dcs->d_kind != DK_EXTERN && !is_do_while)
4383 check_statement_reachable();
4384
4385 check_expr_misc(tn, vctx, cond, !cond, false, false, false);
4386 if (tn->tn_op == ASSIGN && !tn->tn_parenthesized) {
4387 if (hflag && cond)
4388 /* assignment in conditional context */
4389 warning(159);
4390 } else if (tn->tn_op == CON) {
4391 if (hflag && cond && !constcond_flag &&
4392 !tn->tn_system_dependent &&
4393 !(is_do_while &&
4394 is_constcond_false(tn, tn->tn_type->t_tspec)))
4395 /* constant in conditional context */
4396 warning(161);
4397 }
4398 if (!modtab[tn->tn_op].m_has_side_effect) {
4399 /*
4400 * for left operands of COMMA this warning is already
4401 * printed
4402 */
4403 if (tn->tn_op != COMMA && !vctx && !cond)
4404 check_null_effect(tn);
4405 }
4406 debug_node(tn);
4407
4408 /* free the tree memory */
4409 if (dofreeblk)
4410 expr_free_all();
4411 }
4412
4413 /*
4414 * Checks the range of array indices, if possible.
4415 * amper is set if only the address of the element is used. This
4416 * means that the index is allowed to refer to the first element
4417 * after the array.
4418 */
4419 static void
4420 check_array_index(tnode_t *tn, bool amper)
4421 {
4422 const tnode_t *ln = tn->tn_left;
4423 const tnode_t *rn = tn->tn_right;
4424
4425 /* We can only check constant indices. */
4426 if (rn->tn_op != CON)
4427 return;
4428
4429 /* Return if the left node does not stem from an array. */
4430 if (ln->tn_op != ADDR)
4431 return;
4432 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
4433 return;
4434 if (ln->tn_left->tn_type->t_tspec != ARRAY)
4435 return;
4436
4437 /*
4438 * For incomplete array types, we can print a warning only if
4439 * the index is negative.
4440 */
4441 if (is_incomplete(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
4442 return;
4443
4444 /* Get the size of one array element */
4445 int elsz = length_in_bits(ln->tn_type->t_subt, NULL);
4446 if (elsz == 0)
4447 return;
4448 elsz /= CHAR_SIZE;
4449
4450 /* Change the unit of the index from bytes to element size. */
4451 int64_t con;
4452 if (is_uinteger(rn->tn_type->t_tspec))
4453 con = (uint64_t)rn->tn_val->v_quad / elsz;
4454 else
4455 con = rn->tn_val->v_quad / elsz;
4456
4457 int dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
4458
4459 if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
4460 /* array subscript cannot be negative: %ld */
4461 warning(167, (long)con);
4462 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
4463 /* array subscript cannot be > %d: %ld */
4464 warning(168, dim - 1, (long)con);
4465 }
4466 }
4467
4468 static void
4469 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
4470 {
4471 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4472 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4473 if (!szof)
4474 mark_as_set(ln->tn_sym);
4475 mark_as_used(ln->tn_sym, fcall, szof);
4476 }
4477 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4478 /* check the range of array indices */
4479 check_array_index(ln->tn_left, true);
4480 }
4481
4482 static void
4483 check_expr_load(const tnode_t *ln)
4484 {
4485 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4486 /* check the range of array indices */
4487 check_array_index(ln->tn_left, false);
4488 }
4489
4490 static void
4491 check_expr_side_effect(const tnode_t *ln, bool szof)
4492 {
4493 dinfo_t *di;
4494
4495 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4496 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4497 scl_t sc = ln->tn_sym->s_scl;
4498 /*
4499 * Look if there was a asm statement in one of the
4500 * compound statements we are in. If not, we don't
4501 * print a warning.
4502 */
4503 for (di = dcs; di != NULL; di = di->d_enclosing) {
4504 if (di->d_asm)
4505 break;
4506 }
4507 if (sc != EXTERN && sc != STATIC &&
4508 !ln->tn_sym->s_set && !szof && di == NULL) {
4509 /* '%s' may be used before set */
4510 warning(158, ln->tn_sym->s_name);
4511 mark_as_set(ln->tn_sym);
4512 }
4513 mark_as_used(ln->tn_sym, false, false);
4514 }
4515 }
4516
4517 static void
4518 check_expr_assign(const tnode_t *ln, bool szof)
4519 {
4520 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4521 if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
4522 mark_as_set(ln->tn_sym);
4523 if (ln->tn_sym->s_scl == EXTERN)
4524 outusg(ln->tn_sym);
4525 }
4526 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4527 /* check the range of array indices */
4528 check_array_index(ln->tn_left, false);
4529 }
4530
4531 static void
4532 check_expr_call(const tnode_t *tn, const tnode_t *ln,
4533 bool szof, bool vctx, bool cond, bool retval_discarded)
4534 {
4535 lint_assert(ln->tn_op == ADDR);
4536 lint_assert(ln->tn_left->tn_op == NAME);
4537 if (!szof &&
4538 !is_compiler_builtin(ln->tn_left->tn_sym->s_name))
4539 outcall(tn, vctx || cond, retval_discarded);
4540 }
4541
4542 static bool
4543 check_expr_op(const tnode_t *tn, op_t op, const tnode_t *ln,
4544 bool szof, bool fcall, bool vctx, bool cond,
4545 bool retval_discarded, bool eqwarn)
4546 {
4547 switch (op) {
4548 case ADDR:
4549 check_expr_addr(ln, szof, fcall);
4550 break;
4551 case LOAD:
4552 check_expr_load(ln);
4553 /* FALLTHROUGH */
4554 case PUSH:
4555 case INCBEF:
4556 case DECBEF:
4557 case INCAFT:
4558 case DECAFT:
4559 case ADDASS:
4560 case SUBASS:
4561 case MULASS:
4562 case DIVASS:
4563 case MODASS:
4564 case ANDASS:
4565 case ORASS:
4566 case XORASS:
4567 case SHLASS:
4568 case SHRASS:
4569 case REAL:
4570 case IMAG:
4571 check_expr_side_effect(ln, szof);
4572 break;
4573 case ASSIGN:
4574 check_expr_assign(ln, szof);
4575 break;
4576 case CALL:
4577 check_expr_call(tn, ln, szof, vctx, cond, retval_discarded);
4578 break;
4579 case EQ:
4580 if (hflag && eqwarn)
4581 /* operator '==' found where '=' was expected */
4582 warning(160);
4583 break;
4584 case CON:
4585 case NAME:
4586 case STRING:
4587 return false;
4588 default:
4589 break;
4590 }
4591 return true;
4592 }
4593
4594 /*
4595 * vctx ???
4596 * cond whether the expression is a condition that
4597 * will be compared with 0
4598 * eqwarn whether the operator '==' might be a
4599 * misspelled '='
4600 * fcall whether the expression is a function call
4601 * retval_discarded whether the return value of a function call
4602 * is discarded; such calls will be analyzed by
4603 * lint2 in messages 4, 8 and 9
4604 * szof whether the expression is part of a sizeof
4605 * expression, which means that its value is
4606 * discarded since only the type is relevant
4607 */
4608 void
4609 check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
4610 bool eqwarn, bool fcall, bool retval_discarded, bool szof)
4611 {
4612 tnode_t *ln, *rn;
4613 const mod_t *mp;
4614 op_t op;
4615 bool cvctx, ccond, eq, discard;
4616
4617 if (tn == NULL)
4618 return;
4619
4620 ln = tn->tn_left;
4621 rn = tn->tn_right;
4622 mp = &modtab[op = tn->tn_op];
4623
4624 if (!check_expr_op(tn, op, ln,
4625 szof, fcall, vctx, cond, retval_discarded, eqwarn))
4626 return;
4627
4628 cvctx = mp->m_value_context;
4629 ccond = mp->m_compares_with_zero;
4630 eq = mp->m_warn_if_operand_eq &&
4631 !ln->tn_parenthesized &&
4632 rn != NULL && !rn->tn_parenthesized;
4633
4634 /*
4635 * values of operands of ':' are not used if the type of at least
4636 * one of the operands (for gcc compatibility) is void
4637 * XXX test/value context of QUEST should probably be used as
4638 * context for both operands of COLON
4639 */
4640 if (op == COLON && tn->tn_type->t_tspec == VOID)
4641 cvctx = ccond = false;
4642 discard = op == CVT && tn->tn_type->t_tspec == VOID;
4643 check_expr_misc(ln, cvctx, ccond, eq, op == CALL, discard, szof);
4644
4645 switch (op) {
4646 case PUSH:
4647 if (rn != NULL)
4648 check_expr_misc(rn, false, false, eq, false, false,
4649 szof);
4650 break;
4651 case LOGAND:
4652 case LOGOR:
4653 check_expr_misc(rn, false, true, eq, false, false, szof);
4654 break;
4655 case COLON:
4656 check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
4657 break;
4658 case COMMA:
4659 check_expr_misc(rn, vctx, cond, false, false, false, szof);
4660 break;
4661 default:
4662 if (mp->m_binary)
4663 check_expr_misc(rn, true, false, eq, false, false,
4664 szof);
4665 break;
4666 }
4667 }
4668
4669 /*
4670 * Return whether the expression can be used for static initialization.
4671 *
4672 * Constant initialization expressions must be constant or an address
4673 * of a static object with an optional offset. In the first case,
4674 * the result is returned in *offsp. In the second case, the static
4675 * object is returned in *symp and the offset in *offsp.
4676 *
4677 * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
4678 * CON. Type conversions are allowed if they do not change binary
4679 * representation (including width).
4680 *
4681 * C99 6.6 "Constant expressions"
4682 * C99 6.7.8p4 restricts initializers for static storage duration
4683 */
4684 bool
4685 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
4686 {
4687 const sym_t *sym;
4688 ptrdiff_t offs1, offs2;
4689 tspec_t t, ot;
4690
4691 switch (tn->tn_op) {
4692 case MINUS:
4693 if (tn->tn_right->tn_op == CVT)
4694 return constant_addr(tn->tn_right, symp, offsp);
4695 else if (tn->tn_right->tn_op != CON)
4696 return false;
4697 /* FALLTHROUGH */
4698 case PLUS:
4699 offs1 = offs2 = 0;
4700 if (tn->tn_left->tn_op == CON) {
4701 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
4702 if (!constant_addr(tn->tn_right, &sym, &offs2))
4703 return false;
4704 } else if (tn->tn_right->tn_op == CON) {
4705 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
4706 if (tn->tn_op == MINUS)
4707 offs2 = -offs2;
4708 if (!constant_addr(tn->tn_left, &sym, &offs1))
4709 return false;
4710 } else {
4711 return false;
4712 }
4713 *symp = sym;
4714 *offsp = offs1 + offs2;
4715 return true;
4716 case ADDR:
4717 if (tn->tn_left->tn_op == NAME) {
4718 *symp = tn->tn_left->tn_sym;
4719 *offsp = 0;
4720 return true;
4721 } else {
4722 /*
4723 * If this would be the front end of a compiler we
4724 * would return a label instead of 0, at least if
4725 * 'tn->tn_left->tn_op == STRING'.
4726 */
4727 *symp = NULL;
4728 *offsp = 0;
4729 return true;
4730 }
4731 case CVT:
4732 t = tn->tn_type->t_tspec;
4733 ot = tn->tn_left->tn_type->t_tspec;
4734 if ((!is_integer(t) && t != PTR) ||
4735 (!is_integer(ot) && ot != PTR)) {
4736 return false;
4737 }
4738 #if 0
4739 /*
4740 * consider:
4741 * struct foo {
4742 * unsigned char a;
4743 * } f = {
4744 * (unsigned char)(unsigned long)
4745 * (&(((struct foo *)0)->a))
4746 * };
4747 * since psize(unsigned long) != psize(unsigned char),
4748 * this fails.
4749 */
4750 else if (psize(t) != psize(ot))
4751 return -1;
4752 #endif
4753 return constant_addr(tn->tn_left, symp, offsp);
4754 default:
4755 return false;
4756 }
4757 }
4758
4759 /* Append s2 to s1, then free s2. */
4760 strg_t *
4761 cat_strings(strg_t *s1, strg_t *s2)
4762 {
4763
4764 if (s1->st_char != s2->st_char) {
4765 /* cannot concatenate wide and regular string literals */
4766 error(292);
4767 return s1;
4768 }
4769
4770 size_t len1 = s1->st_len;
4771 size_t len2 = s2->st_len;
4772 size_t chsize = s1->st_char ? sizeof(char) : sizeof(wchar_t);
4773 size_t size1 = len1 * chsize;
4774 size_t size2 = (len2 + 1) * chsize;
4775 s1->st_mem = xrealloc(s1->st_mem, size1 + size2);
4776 memcpy((char *)s1->st_mem + size1, s2->st_mem, size2);
4777 free(s2->st_mem);
4778
4779 s1->st_len = len1 + len2;
4780 free(s2);
4781
4782 return s1;
4783 }
4784
4785
4786 typedef struct stmt_expr {
4787 memory_pool se_mem;
4788 sym_t *se_sym;
4789 struct stmt_expr *se_enclosing;
4790 } stmt_expr;
4791
4792 static stmt_expr *stmt_exprs;
4793
4794 void
4795 begin_statement_expr(void)
4796 {
4797 stmt_expr *se = xmalloc(sizeof(*se));
4798 se->se_mem = expr_save_memory();
4799 se->se_sym = NULL;
4800 se->se_enclosing = stmt_exprs;
4801 stmt_exprs = se;
4802 }
4803
4804 void
4805 do_statement_expr(tnode_t *tn)
4806 {
4807 block_level--;
4808 mem_block_level--;
4809 stmt_exprs->se_sym = tn != NULL
4810 ? mktempsym(block_dup_type(tn->tn_type))
4811 : NULL; /* after a syntax error */
4812 mem_block_level++;
4813 block_level++;
4814 /* ({ }) is a GCC extension */
4815 gnuism(320);
4816 }
4817
4818 tnode_t *
4819 end_statement_expr(void)
4820 {
4821 stmt_expr *se = stmt_exprs;
4822 if (se->se_sym == NULL)
4823 return NULL; /* after a syntax error */
4824 tnode_t *tn = build_name(se->se_sym, false);
4825 (void)expr_save_memory(); /* leak */
4826 expr_restore_memory(se->se_mem);
4827 stmt_exprs = se->se_enclosing;
4828 free(se);
4829 return tn;
4830 }
4831