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