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