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