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